如何创建圆形频率直方图

2024-05-23 21:50:19 发布

您现在位置:Python中文网/ 问答频道 /正文

亲爱的stackoverflow会员:

我想创建一个循环频率直方图(玫瑰图),使用文本文件中作为单个列列出的每个箱子的频率。如何在python3中使用matplotlib.pyplot和numpy实现这一点

我在互联网上找到了一个代码,我做了一个初步尝试,但是当我得到玫瑰图时,这些垃圾桶重叠了,而它们应该在一起。其他细节:每个箱子的圆半径应该是频率,但这也会改变,与我的频率不匹配。 我希望我的箱子从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

enter image description here


Tags: 代码numpytxtmatplotlib互联网直方图stackoverflowpython3
1条回答
网友
1楼 · 发布于 2024-05-23 21:50:19

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

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()

polar bar plot

相关问题 更多 >