【发布时间】:2018-08-16 07:07:20
【问题描述】:
我正在使用 Matplotlib 创建一个极坐标直方图。直方图的正确数据(以弧度为单位)如下:
对齐方式为 [0,0.78) 弧度(又名 0 到 45 度)[0.78,...)(45 到 90 度)等。
但是,当使用极坐标图绘制它时,bin 现在以 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 是以弧度为单位的角度列表
极地
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()
完整代码
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()
谢谢
【问题讨论】:
-
分享完整的最小工作代码以重现您的数字。目前尚不清楚
bins_number是什么以及angles是什么。提供所有变量 -
添加完整代码
-
如果不深入研究,我猜你可以为条形图设置
align="left"。 -
是的,看起来你是对的。解决此问题的确切代码是:
align='edge'用于条形图。感谢您的帮助
标签: python matplotlib histogram