如何创建圆形频率直方图

How to create a circular frequency histogram

亲爱的 Whosebug 会员,

我想创建一个圆形频率直方图(玫瑰图),使用在文本文件中列为单列的每个 bin 的频率。我怎么能在 python3 中使用 matplotlib.pyplot 和 numpy 来做到这一点?

我最初尝试使用在 Internet 上找到的代码,但是当我得到玫瑰图时,当它们应该彼此相邻时,它们重叠了。其他细节:每个 bin 的圆半径应该是频率,但这也会改变并且与我的频率不匹配。 我希望我的箱子从 0 度到 360 度,宽度为 10 度;示例:0-10、10-20 等

这是频率为 (frequencies.txt) 的 txt 文件示例:

0
0
0
0
0
2
0
1
1
0
1
0
0
1
2
29
108
262
290
184
81
25
7
2
3
1
1
0
0
0
0
0
0
0
0
0

您可以创建极坐标条形图。角度需要从度数转换为弧度。

frequencies = np.loadtxt('filename.txt') 将从文件 (docs) 中读取值。

import numpy as np
import matplotlib.pyplot as plt

frequencies = [0, 0, 0, 0, 0, 2, 0, 1, 1, 0, 1, 0, 0, 1, 2, 29, 108, 262, 290,
               184, 81, 25, 7, 2, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

fig = plt.figure()
ax = plt.axes(polar=True)

theta = np.radians(np.arange(0, 360, 10))
width = np.radians(10)
ax.bar(theta, frequencies, width=width,
       facecolor='lightblue', edgecolor='red', alpha=0.5, align='edge')
ax.set_xticks(theta)
plt.show()