我正在使用Matplotlib创建一个极性柱状图。直方图的正确数据(弧度)如下:
线形为[0,0.78)弧度(即0至45度)[0.78,…)(45至90度)等
但是,使用极坐标图绘制时,箱子现在以0为中心,而不是从0开始。但是直方图计数是一样的。在
如果真的是(-22.5度,22.5度),那么直方图分布就会不同。因此,极坐标图轴标签似乎不正确-也就是说,0度标签实际上应该是22.5度(或者0度标签应该顺时针移动22.5度)。在
有没有人知道如何做到这一点?在
相关代码:
直方图bins = np.linspace(-np.pi, np.pi, bins_number + 1)
n, _, _ = plt.hist(angles, bins) # Create histogram
plt.show()
注意,angles是以弧度表示的角度列表
极性
^{pr2}$
完整代码import numpy as np
import matplotlib.pyplot as plt
import csv
with open('circ2.csv', 'r') as f:
reader=csv.reader(f)
angles=[] # Initialise empty list
next(reader) # Skip header line
for row in reader:
angle = float(row[1]) # Angle is in the second column of the row
angles.append(angle)
bins_number = 8 # the [-180, 180) interval will be subdivided into this
bins = np.linspace(-np.pi, np.pi, bins_number + 1)
n, _, _ = plt.hist(angles, bins) # Create histogram
plt.clf()
width = 2 * np.pi / bins_number
ax = plt.subplot(1, 1, 1, projection='polar')
bars = ax.bar(bins[:bins_number], n, width=width, bottom=0.0)
for bar in bars:
bar.set_alpha(0.5)
plt.show()
谢谢