你想要一个条形图:
import numpy as np
import matplotlib.pyplot as plt
x = np.sort(np.random.rand(6))
y = np.random.rand(5)
plt.bar(x[:-1], y, width=x[1:] - x[:-1], align='edge')
plt.show()
这里x
包含条的边缘并y
包含高度(不是区域!)。请注意,inx
比 in多一个元素,y
因为边比条数多一个。

用原始数据和面积计算:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
frequencies = np.array([2, 7, 21, 15, 12])
bins = np.array([65, 75, 80, 90, 105, 110])
widths = bins[1:] - bins[:-1]
heights = frequencies/widths
plt.bar(bins[:-1], heights, width=widths, align='edge')
plt.show()
