您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何绘制直方图,以使matplotlib中的条形高度之和为1?

如何绘制直方图,以使matplotlib中的条形高度之和为1?

如果您提出了更完整的工作示例(或在这种情况下为非工作示例),则将更有帮助。

我尝试了以下方法

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

其他 2022/1/1 18:38:26 有446人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶