Python |用边界和频率绘制直方图

2024-04-26 20:42:34 发布

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

我已经计算了区间边界

import numpy as np

boundaries = np.array([-1.0, -0.5, 0.0, 0.5, 1.0])

和归一化频率

normalized_frequencies = np.array([0.10, 0.40, 0.40, 0.10])

My question: How can I plot a histogram with the bar boundaries like boundaries and the y-values as normalized_frequencies?


Tags: theimportnumpymyasnparrayhow
1条回答
网友
1楼 · 发布于 2024-04-26 20:42:34

我想你需要的是条形图而不是柱状图。这里的要点是只使用前四个边界值,然后将宽度等于边界之间的间距。在这种情况下,宽度为0.5。黑色edgecolor是用来区分条形图的。你知道吗

import numpy as np

boundaries = np.array([-1.0, -0.5, 0.0, 0.5, 1.0])
normalized_frequencies = np.array([0.10, 0.40, 0.40, 0.10])
width = boundaries[1] - boundaries[0]

plt.bar(boundaries[0:-1], normalized_frequencies, width=width, 
        align='edge', edgecolor='k')

enter image description here

第二种选择是找到每个间隔的中心点

centers = (boundaries[1:] + boundaries[:-1]) / 2
width = centers[1] - centers[0]

plt.bar(centers, normalized_frequencies, width=width, 
        align='edge', edgecolor='k')

相关问题 更多 >