我正在尝试使用Matplotlib在极坐标中创建二维彩色直方图。
箱边缘将沿径向轴均匀间隔,但在给定半径处沿角度轴将存在任意数量的箱。 我正在尝试这个,因为我希望随着半径的增加,箱子的大小相对恒定。 所以在一个很小的半径范围内,只有几个角度箱,但是在更大的半径范围内,会有更多的角度箱。
这是我第一次使用pcolormesh尝试(尽管对我来说似乎有点费解)。 如您所见,在箱边缘周围有白色区域,因为即使在极坐标中,网格线也保持笔直。 (嗯,我的第一篇文章,所以我无法附上图片。)
这是link。
有没有办法让白色区域消失(最好是通过制作同心圆形边框边缘)?
import matplotlib.pyplot as plt
import numpy as np
# bin properties in radial coordinate
nBins_r = 2 # number of bins in radial direction
r_max = 1.0
r_min = 0.0
delta_r = (r_max - r_min)/float(nBins_r) # size of increment in r
# bin properties in angular (theta) coordinate
nBins_theta_list = [] # number of bins depends on radius
delta_circumf = delta_r # set bin edge length same in both coordinates
circumf_max = 2.0*np.pi*r_max
nBins_theta_max = int(circumf_max/delta_circumf) # max number of bins in theta needed
# fill radial bin edges
binEdges_r = [0.0]
for i in xrange(nBins_r):
binEdges_r.append(float(i+1)*delta_r)
binEdges_r.append(float(i+1)*delta_r) # do twice for next bin in r
binEdges_r.pop() # last entry does not need to be repeated
# find size of array needed for angular bin edges, fill later
binEdges_theta = np.zeros(nBins_theta_max+1)
# create meshgrid
thetas, rs = np.meshgrid(binEdges_theta, binEdges_r, indexing='xy')
# fill matrix of angular bin edges
for i_r in xrange(nBins_r): # iterate through different r's
r_edge = float(i_r+1)*delta_r # current radius
circumf = 2.0*np.pi**r_edge # current circumference at radius r
nBins_theta = int(circumf/delta_circumf) # number of bins in theta
delta_theta = 2.0*np.pi/nBins_theta
for i_theta in xrange(nBins_theta+1):
thetas[2*i_r, i_theta] = i_theta*delta_theta - 0.5*delta_theta
thetas[2*i_r+1, i_theta] = i_theta*delta_theta - 0.5*delta_theta # do twice for next bin in r
nBins_theta_list.append(nBins_theta)
# weights of bins to be colored
w = np.full((2*nBins_r, nBins_theta_max), -1)
temp = 0.0
for i in xrange(nBins_r):
for j in xrange(nBins_theta_list[i]):
temp += 1
w[2*i, j] = temp # set some arbitrary weight
ws = np.ma.masked_values(w, -1)
# create plot
ax = plt.subplot(projection="polar")
ax.pcolormesh(thetas, rs, ws)
plt.show()