用matplotlib在极地图中遮蔽“细胞”

时间:2012-05-31 16:09:36

标签: python plot matplotlib

我有一堆规则分布的点(θ= n *π/ 6,r = 1 ... 8),每个点的值都为[0,1]。我可以使用

在matplotlib中使用它们的值绘制它们
polar(thetas, rs, c=values)

而是只有一个微小的点,我想用对应于点值的颜色遮蔽相应的“细胞”(即一切直到相邻点的一半):

Polar plot with shaded cells

(注意,这里我的值只是[0,.5,1],实际上它们将是介于0和1之间的所有东西。有没有直接的方法来实现这个(或者足够接近的东西)matplotlib?也许更容易将其视为二维直方图?

3 个答案:

答案 0 :(得分:12)

这可以通过将其视为极化堆积条形图来完成:

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()

产地:

enter image description here

答案 1 :(得分:11)

当然!只需在极轴上使用pcolormesh

E.g。

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()

enter image description here

如果您的数据不在常规网格中,那么您需要将其网格化以使用pcolormesh。

然而,看起来它在你的情节中的常规网格上。在这种情况下,网格化非常简单。如果它已经订购,可能就像调用reshape一样简单。否则,一个简单的循环或利用您的numpy.histogram2d值作为权重的z将满足您的需求。

答案 2 :(得分:3)

嗯,总的来说它是相当粗糙的,但是这里有一个完整的部分。

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()