11

規則的に分散されたポイント(θ= n*π/6、r = 1 ... 8)がたくさんあり、それぞれが[0、1]の値を持っています。matplotlibでそれらの値を使用してそれらをプロットできます

polar(thetas, rs, c=values)

しかし、ほんのわずかな小さな点を持っているのではなく、対応する「セル」(つまり、隣接するポイントの途中までのすべて)をポイントの値に対応する色でシェーディングしたいと思います。

影付きのセルを使用した極座標プロット

(ここで私の値は[0、.5、1]であることに注意してください。実際には、0から1までのすべてになります。matplotlibでこれ(または十分に近いもの)を実現する簡単な方法はありますか?それを2Dヒストグラムとして考えるには?

4

3 に答える 3

13

これは、極積み棒グラフとして扱うことで非常にうまく行うことができます。

import matplotlib.pyplot as plt
import numpy as np
from random import choice

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)

for i in xrange(12*8):
    color = choice(['navy','maroon','lightgreen'])
    ax.bar(i * 2 * np.pi / 12, 1, width=2 * np.pi / 12, bottom=i / 12,
           color=color, edgecolor = color)
plt.ylim(0,10)
ax.set_yticks([])
plt.show()

生産:

ここに画像の説明を入力してください

于 2012-06-01T10:22:56.170 に答える
11

もちろん!pcolormesh極軸で使用するだけです。

例えば

import matplotlib.pyplot as plt
import numpy as np

# Generate some data...
# Note that all of these are _2D_ arrays, so that we can use meshgrid
# You'll need to "grid" your data to use pcolormesh if it's un-ordered points
theta, r = np.mgrid[0:2*np.pi:20j, 0:1:10j]
z = np.random.random(theta.size).reshape(theta.shape)


fig, (ax1, ax2) = plt.subplots(ncols=2, subplot_kw=dict(projection='polar'))


ax1.scatter(theta.flatten(), r.flatten(), c=z.flatten())
ax1.set_title('Scattered Points')

ax2.pcolormesh(theta, r, z)
ax2.set_title('Cells')

for ax in [ax1, ax2]:
    ax.set_ylim([0, 1])
    ax.set_yticklabels([])

plt.show()

ここに画像の説明を入力してください

データがまだ通常のグリッド上にない場合は、pcolormeshを使用するためにデータをグリッド化する必要があります。

ただし、プロットからは通常のグリッド上にあるように見えます。その場合、グリッド化は非常に簡単です。すでに注文されている場合は、を呼び出すのと同じくらい簡単かもしれませんreshape。それ以外の場合は、単純なループまたは重みとしての値を利用numpy.histogram2dして、必要な処理を実行します。z

于 2012-05-31T17:38:47.180 に答える
4

まあ、それは全体的にかなり洗練されていませんが、ここにセクションを締めくくるバージョンがあります。

from matplotlib.pylab import *
ax = subplot(111, projection='polar')

# starts grid and colors
th = array([pi/6 * n for n in range(13)]) # so n = 0..12, allowing for full wrapping
r = array(range(9)) # r = 0..8
c = array([[random_integers(0, 10)/10 for y in range(th.size)] for x in range(r.size)])

# The smoothing
TH = cbook.simple_linear_interpolation(th, 10)

# Properly padding out C so the colors go with the right sectors (can't remember the proper word for such segments of wedges)
# A much more elegant version could probably be created using stuff from itertools or functools
C = zeros((r.size, TH.size))
oldfill = 0
TH_ = TH.tolist()

for i in range(th.size):
    fillto = TH_.index(th[i])

    for j, x in enumerate(c[:,i]):
        C[j, oldfill:fillto].fill(x)

    oldfill = fillto

# The plotting
th, r = meshgrid(TH, r)
ax.pcolormesh(th, r, C)
show()
于 2012-05-31T20:19:51.217 に答える