[英]Using matplotlib how could I plot a histogram with given data in python
[英]How do I plot a standard histogram data to a polar histogram using Python and pyplot?
我有一个角度列表,以度为单位。 我想显示一个极坐标直方图,其中[0°,360°]值的范围被细分为相等的区间,并显示角度列表中有多少值落入每个区域。 我使用以下代码获得直方图数据(我已经检查过它是正确的):
bins_number = 8 # the [0, 360) interval will be subdivided into this number of equal bins
bins = np.linspace(0.0, 360.0, bins_number + 1)
n, _, _ = plt.hist(angles, bins)
现在,我尝试使用以下代码将此数据绘制成极坐标直方图:
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
n_numbers = 100
bins_number = 8 # the [0, 360) interval will be subdivided into this
# number of equal bins
bins = np.linspace(0.0, 2 * np.pi, bins_number + 1)
angles = 2 * np.pi * np.random.rand(n_numbers)
n, _, _ = plt.hist(angles, bins)
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
degrees = np.random.randint(0, 360, size=200)
radians = np.deg2rad(degrees)
bin_size = 20
a , b=np.histogram(degrees, bins=np.arange(0, 360+bin_size, bin_size))
centers = np.deg2rad(np.ediff1d(b)//2 + b[:-1])
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection='polar')
ax.bar(centers, a, width=np.deg2rad(bin_size), bottom=0.0, color='.8', edgecolor='k')
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.