简体   繁体   English

Polar Contour Plot-分箱形状不匹配

[英]Polar Contour Plot - binning shape mismatch

I would like to plot a polar contour-map of azimuthal and zenith angles obtained from XYZ data. 我想绘制从XYZ数据获得的方位角和天顶角的极坐标轮廓图。 But the arrays I pass into the contourf function are misshapen and I'm not sure how to correct this? 但是我传递给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()

The code runs but throws the following warning: Shape of x does not match that of z: found (13, 7) instead of (12, 6). 该代码运行,但引发以下警告: x的形状与z的形状不匹配:找到(13,7)而不是(12,6)。

Now I think I understand the error. 现在我想我理解了错误。 The bins are 13 for azimuths (0-360) and 7 for zeniths (0-180). 方位角(0-360)的垃圾箱为13个,天顶(0-180)的垃圾箱为7个。 The matrix returned by histogram2d function has a shape of (12,6) because this is the number of slots between the edges. 直方图2d函数返回的矩阵的形状为(12,6),因为这是边缘之间的缝隙数。 I'm just not sure how to fix the binning. 我只是不确定如何解决合并。

One way is to expand the grid array to be the same shape as theta and r . 一种方法是将grid阵列扩展为与thetar相同的形状。 This is necessary so that the polar plot extends all the way around (and matches up at theta=0 . 这是必要的,这样极坐标图就可以一直延伸(并在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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM