根据升序排列的频率数组绘制直方图
Plot an histogram from an array of frequencies ordered by ascending values
我想绘制一个直方图,X 轴为高度,Y 轴为该高度的频率。我的数据集看起来像那样
[10000,200,3000,5400,...]
第一个数字是身高170cm第二个171cm的人数...
我想用第一个箱子制作一个直方图,显示有多少人身高在 170 厘米到 175 厘米之间,第二个箱子是 175-180 ...
通常,直方图会计算其输入数组中有多少进入了一组 bin 中的每一个。然后创建条形图。
因为你已经有了频率,你可以直接创建一个条形图,x 轴为高度,y 轴为频率。
要获得每 5 个频率,只需将 5 个后续高度频率相加即可。
下面是一些代码来说明这些概念:
import matplotlib.pyplot as plt
min_height = 153
max_height = 191
height_freq = [1, 2, 2, 4, 5, 11, 52, 53, 113, 193, 292, 442, 550, 792, 983, 1311, 1553, 1735, 1949, 1974, 2082,
2031, 1852, 1591, 1416, 1142, 816, 729, 478, 333, 211, 125, 78, 49, 20, 15, 11, 2, 2]
fig, ax = plt.subplots(ncols=2, figsize=(12, 4))
heights = range(min_height, max_height + 1)
ax[0].bar(heights, height_freq, color='crimson', edgecolor='white')
ax[0].set_ylabel('height frequency')
heights_per_5 = range(min_height, max_height + 1, 5)
height_freq_per_5 = [sum(height_freq[h - min_height:h - min_height + 5]) for h in heights_per_5]
ax[1].bar(heights_per_5, height_freq_per_5, width=4.5, align='edge', color='limegreen', edgecolor='white')
ax[1].set_ylabel('height frequency (per 5 cm)')
plt.show()
我想绘制一个直方图,X 轴为高度,Y 轴为该高度的频率。我的数据集看起来像那样
[10000,200,3000,5400,...]
第一个数字是身高170cm第二个171cm的人数... 我想用第一个箱子制作一个直方图,显示有多少人身高在 170 厘米到 175 厘米之间,第二个箱子是 175-180 ...
通常,直方图会计算其输入数组中有多少进入了一组 bin 中的每一个。然后创建条形图。
因为你已经有了频率,你可以直接创建一个条形图,x 轴为高度,y 轴为频率。
要获得每 5 个频率,只需将 5 个后续高度频率相加即可。
下面是一些代码来说明这些概念:
import matplotlib.pyplot as plt
min_height = 153
max_height = 191
height_freq = [1, 2, 2, 4, 5, 11, 52, 53, 113, 193, 292, 442, 550, 792, 983, 1311, 1553, 1735, 1949, 1974, 2082,
2031, 1852, 1591, 1416, 1142, 816, 729, 478, 333, 211, 125, 78, 49, 20, 15, 11, 2, 2]
fig, ax = plt.subplots(ncols=2, figsize=(12, 4))
heights = range(min_height, max_height + 1)
ax[0].bar(heights, height_freq, color='crimson', edgecolor='white')
ax[0].set_ylabel('height frequency')
heights_per_5 = range(min_height, max_height + 1, 5)
height_freq_per_5 = [sum(height_freq[h - min_height:h - min_height + 5]) for h in heights_per_5]
ax[1].bar(heights_per_5, height_freq_per_5, width=4.5, align='edge', color='limegreen', edgecolor='white')
ax[1].set_ylabel('height frequency (per 5 cm)')
plt.show()