Python 需要 Matplotlib x_ticks 没有出现在雷达图上
Python Matplotlib required x_ticks don't appear on a radar chart
好的,我正在用以下风向数据绘制雷达图:
direction = ['N', 'NW', 'E', 'N', 'N', 'NW', 'NW', 'N', 'N', 'W', 'N', 'NW', 'NW', 'NW', 'N', 'SE', 'NE', 'E', 'E', 'NW', 'NE', 'S', 'E', 'W', 'W', 'NW', 'W', 'W', 'W', 'W', 'W', 'W', 'NW', 'W', 'W', 'W', 'W', 'W', 'W', 'NW', 'W', 'N', 'SE', 'E', 'NE', 'E', 'NE']
我收集了图表上价格变动的属性列表:
attributes = ['E', 'NE', 'N', 'NW', 'W', 'SW', 'S', 'SE']
然后我列出了对应于属性中每个方向的值和轴的角度来绘制数据:
values = [6, 4, 8, 10, 16, 0, 1, 2, 6]
angles = [n / float(len(attributes)) * 2 * math.pi for n in range(len(attributes)[![enter image description here][1]][1])]
angles += angles [:1]
然后作图:
ax3 = plt.subplot(111, polar=True)
plt.xticks(angles[:-1], attributes)
ax3.plot(angles,values)
ax3.fill(angles, values, 'teal', alpha=0.1)
ax3.set_title("Direction")
plt.show()
而且效果很好(参见 pic_1)。但是当我在我的项目中使用相同的东西时,图表上没有出现刻度(参见 pic_2)!这是代码(图中有 4 个子批次,雷达图是 ax3 - 右下角):
fig = plt.figure(figsize=(12, 8))
ax1 = plt.subplot2grid(gridsize, (0, 0))
ax2 = plt.subplot2grid(gridsize, (1, 0))
ax3 = plt.subplot2grid(gridsize, (1, 1), polar=True)
ax4 = plt.subplot2grid(gridsize, (0, 1))
[...ax1...]
[...ax2...]
[...ax4...]
ax3.set_xticks(angles[:-1], attributes)
ax3.plot(angles, values)
ax3.fill(angles, values, 'teal', alpha=0.1)
ax3.set_title('Направление ветра')
plt.show()
怎么了?
如果您要将刻度和刻度标签添加到坐标轴(而不是使用 pyplot.xticks
,如您的第一个片段),您需要分别分配刻度位置和标签。尝试以下操作,而不是 ax3.set_xticks(angles, attributes)
做:
ax3.set_xticks(angles)
ax3.set_xticklabels(attributes)
好的,我正在用以下风向数据绘制雷达图:
direction = ['N', 'NW', 'E', 'N', 'N', 'NW', 'NW', 'N', 'N', 'W', 'N', 'NW', 'NW', 'NW', 'N', 'SE', 'NE', 'E', 'E', 'NW', 'NE', 'S', 'E', 'W', 'W', 'NW', 'W', 'W', 'W', 'W', 'W', 'W', 'NW', 'W', 'W', 'W', 'W', 'W', 'W', 'NW', 'W', 'N', 'SE', 'E', 'NE', 'E', 'NE']
我收集了图表上价格变动的属性列表:
attributes = ['E', 'NE', 'N', 'NW', 'W', 'SW', 'S', 'SE']
然后我列出了对应于属性中每个方向的值和轴的角度来绘制数据:
values = [6, 4, 8, 10, 16, 0, 1, 2, 6]
angles = [n / float(len(attributes)) * 2 * math.pi for n in range(len(attributes)[![enter image description here][1]][1])]
angles += angles [:1]
然后作图:
ax3 = plt.subplot(111, polar=True)
plt.xticks(angles[:-1], attributes)
ax3.plot(angles,values)
ax3.fill(angles, values, 'teal', alpha=0.1)
ax3.set_title("Direction")
plt.show()
而且效果很好(参见 pic_1)。但是当我在我的项目中使用相同的东西时,图表上没有出现刻度(参见 pic_2)!这是代码(图中有 4 个子批次,雷达图是 ax3 - 右下角):
fig = plt.figure(figsize=(12, 8))
ax1 = plt.subplot2grid(gridsize, (0, 0))
ax2 = plt.subplot2grid(gridsize, (1, 0))
ax3 = plt.subplot2grid(gridsize, (1, 1), polar=True)
ax4 = plt.subplot2grid(gridsize, (0, 1))
[...ax1...]
[...ax2...]
[...ax4...]
ax3.set_xticks(angles[:-1], attributes)
ax3.plot(angles, values)
ax3.fill(angles, values, 'teal', alpha=0.1)
ax3.set_title('Направление ветра')
plt.show()
怎么了?
如果您要将刻度和刻度标签添加到坐标轴(而不是使用 pyplot.xticks
,如您的第一个片段),您需要分别分配刻度位置和标签。尝试以下操作,而不是 ax3.set_xticks(angles, attributes)
做:
ax3.set_xticks(angles)
ax3.set_xticklabels(attributes)