极坐标图分块形状不匹配

2024-09-28 22:33:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想绘制一张从XYZ数据获得的方位角和天顶角的极坐标图。但是我传递给contourf函数的数组是畸形的,我不知道如何纠正这个问题?在

import numpy as np
import matplotlib.pyplot as plt

# Populated arrays with angles.
azimuths = np.random.random(200)*360
zeniths = np.random.random(200)*180

a_bins = np.linspace(0,360,13)
z_bins = np.linspace(0,180,7)

grid, ae, ze = np.histogram2d(azimuths, zeniths, bins=[a_bins,z_bins])

a_bins = np.radians(a_bins)
r, theta = np.meshgrid(z_bins, a_bins)

# Plot
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
cax = ax.contourf(theta, r, grid, 30)
cb = fig.colorbar(cax)

plt.show()

代码运行,但抛出以下警告:x的形状与z的形状不匹配:找到(13,7)而不是(12,6)。

现在我想我理解了这个错误。方位角为13个(0-360),7个为天顶(0-180)。histogram2d函数返回的矩阵的形状为(12,6),因为这是边之间的槽数。我只是不知道怎么修理箱子。在


Tags: 函数importasnppltrandomgrid形状
1条回答
网友
1楼 · 发布于 2024-09-28 22:33:38

一种方法是将grid数组展开为与theta和{}相同的形状。这是必要的,这样极坐标图就可以一直延伸(并且在theta=0处匹配)。在

import numpy as np
import matplotlib.pyplot as plt

# Populated arrays with angles.
azimuths = np.random.random(200)*360
zeniths = np.random.random(200)*180

a_bins = np.linspace(0,360,13)
z_bins = np.linspace(0,180,7)

grid, ae, ze = np.histogram2d(azimuths, zeniths, bins=[a_bins,z_bins])

a_bins = np.radians(a_bins)
r, theta = np.meshgrid(z_bins, a_bins)

# Extend grid by one column row, using the 0th column and row
g = np.zeros(r.shape)
g[:-1,:-1] = grid 
g[-1] = g[0]      # copy the top row to the bottom
g[:,-1] = g[:,0]  # copy the left column to the right
print g.shape,r.shape,theta.shape
### (13, 7) (13, 7) (13, 7)

# Plot
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
cax = ax.contourf(theta, r, g, 30)
cb = fig.colorbar(cax)

plt.show()

enter image description here

相关问题 更多 >