极坐标2D直方图的Matplotlib

3
我正在尝试在极坐标轴上绘制一些带有直方图的数据,但似乎无法正常工作。以下是一个示例,我使用了自定义投影,可以在这里找到它,它适用于散点图,因此我认为我的问题出在直方图函数上。这让我整天都很疯狂,有没有人知道我做错了什么......
import random
import numpy as np
import matplotlib.pyplot as plt

baz = np.zeros((20))
freq = np.zeros((20))
pwr = np.zeros((20))
for x in range(20): 
    baz[x] = random.randint(20,25)*10
    freq[x] = random.randint(1,10)*10
    pwr[x] = random.randint(-10,-1)*10
baz = baz*np.pi/180.  

abins = np.linspace(0,2*np.pi,360)      # 0 to 360 in steps of 360/N.
sbins = np.linspace(1, 100) 
H, xedges, yedges = np.histogram2d(baz, freq, bins=(abins,sbins), weights=pwr)

plt.figure(figsize=(14,14))
plt.subplot(1, 1, 1, projection='northpolar')
#plt.scatter(baz, freq)
plt.pcolormesh(H)
plt.show()

你有想要实现的绘图类型的示例吗?你是在寻找类似于这个示例中的极坐标条形图吗? - gcalmettes
1个回答

3

如果您将一个类似于a bins和sbins的mgrid显式传递给pcolormesh命令,则您的代码可以正常工作。

以下是根据您的代码所做的示例:

import matplotlib.pyplot as plt
import numpy as np

#Generate the data
size = 200

baz = 10*np.random.randint(20, 25, size)*np.pi/180.
freq = 10*np.random.randint(1, 10, size)
pwr = 10*np.random.randint(-10, -1, size)

abins = np.linspace(0, 2*np.pi, 360)      # 0 to 360 in steps of 360/N.
sbins = np.linspace(1, 100, 50) 
H, xedges, yedges = np.histogram2d(baz, freq, bins=(abins,sbins), weights=pwr)

#Grid to plot your data on using pcolormesh
theta, r = np.mgrid[0:2*np.pi:360j, 1:100:50j]

fig, ax = plt.subplots(figsize=(14,14), subplot_kw=dict(projection='northpolar'))
ax.pcolormesh(theta, r, H)

ax.set_yticklabels([]) #remove yticklabels

plt.show()

polar


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接