python - 如何在极坐标图中使用颜色条?

标签 python matplotlib colorbar

我正在尝试在极坐标图中使用颜色条。我没听懂。这是我的简单极坐标图代码。

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

y2 = [96.31883883883884, 105.53807807807806, 96.4374174174175, 94.50975975975975, 82.67206206206207, 125.8472472472473, 126.62304304304303, 124.98103103103091, 117.13329329329326, 109.36357357357365, 107.02209209209208, 117.19069069069054, 96.31883883883884]
y3 = [242.29120120120174, 227.6075375375375, 220.69459459459455, 222.21662324649296, 225.59189189189183, 218.9322422422421, 219.08093093093117, 226.80649649649635, 229.3265965965969, 201.60476476476458, 196.92468468468442, 190.94575575575595, 242.29120120120174]
y4 = [31.625100401606428, 36.0695295295295, 35.989519038076125, 38.314428857715434, 40.236536536536505, 67.78894894894894, 68.63077077077074, 64.99336336336333, 63.45824824824829, 63.41446446446452, 61.0178978978978, 63.99864864864866, 31.625100401606428]

z = np.arange(0, 13)
theta1 = (2*np.pi/12)*z
ax1 = fig.add_axes([0.42, 0.36, 0.18, 0.30], polar=True, axisbg='white')
ax1.plot(theta1, y2, color='black', lw=2)       
ax1.plot(theta1, y3, color='blue', lw=2)        
ax1.plot(theta1, y4, color='green', lw=2)

ax1.set_rmax(500)

ax1.set_theta_zero_location('N')
ax1.set_xticklabels(['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'], color='red')
plt.show()

最佳答案

这个想法是创建数据的pcolormesh图。为此,需要首先定义 theta 和 r 的坐标数组。

import numpy as np
import matplotlib.pyplot as plt

y2 = [96.31883883883884, 105.53807807807806, 96.4374174174175, 94.50975975975975, 82.67206206206207, 125.8472472472473, 126.62304304304303, 124.98103103103091, 117.13329329329326, 109.36357357357365, 107.02209209209208, 117.19069069069054, 96.31883883883884]
y3 = [242.29120120120174, 227.6075375375375, 220.69459459459455, 222.21662324649296, 225.59189189189183, 218.9322422422421, 219.08093093093117, 226.80649649649635, 229.3265965965969, 201.60476476476458, 196.92468468468442, 190.94575575575595, 242.29120120120174]
y4 = [31.625100401606428, 36.0695295295295, 35.989519038076125, 38.314428857715434, 40.236536536536505, 67.78894894894894, 68.63077077077074, 64.99336336336333, 63.45824824824829, 63.41446446446452, 61.0178978978978, 63.99864864864866, 31.625100401606428]

theta = np.linspace(0, 2*np.pi, len(y2))
r = [1,2,3,4]

z = [y2,y3,y4]

fig=plt.figure()
ax = fig.add_subplot(111, polar=True)

pc = ax.pcolormesh(theta, r, z)

fig.colorbar(pc)

ax.set_theta_zero_location('N')
ax.set_xticklabels(['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'], color='red')
plt.show()

enter image description here

关于python - 如何在极坐标图中使用颜色条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55570532/

相关文章:

python - 在 pygtk 中处理双击和单击事件

python - 如何使用 numpy 将 R、G、B 值提取到单独的数组中

Python - 列出文件夹中的文件 - 带变量的路径名

python - matplotlib 中没有网格的极坐标图

python - 使用箭袋绘制矢量场

python - 加入元组列表?

python - 如何在 Pandas 中绘制非内联图像

matlab - 在不删除颜色条的情况下在现有轴内显示新图像

python - Matplotlib:向 cartopy 图像添加颜色条

python - APLpy错误: 'Colorbar' object has no attribute 'set_axis_label_text'