如果您提出了更完整的工作示例(或在这种情况下为非工作示例),则将更有帮助。
我尝试了以下方法:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(1000)
fig = plt.figure()
ax = fig.add_subplot(111)
n, bins, rectangles = ax.hist(x, 50, density=True)
fig.canvas.draw()
plt.show()
实际上,这将产生一个y轴从到的条形图直方图[0,1]
。
此外,按照该hist
文件(即ax.hist?
从ipython
),我觉得总和也没关系:
*normed*:
If *True*, the first element of the return tuple will
be the counts normalized to form a probability density, i.e.,
``n/(len(x)*dbin)``. In a probability density, the integral of
the histogram should be 1; you can verify that with a
trapezoidal integration of the probability density function::
pdf, bins, patches = ax.hist(...)
print np.sum(pdf * np.diff(bins))
在上面的命令后尝试一下:
np.sum(n * np.diff(bins))
我得到1.0
预期的返回值。请记住,normed=True
这并不意味着每个小节的值之和将是统一的,而不是在小节上的积分是统一的。在我的情况下,np.sum(n)
返回约7.2767
。