栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

我尝试了以下方法:

import numpy as npimport matplotlib.pyplot as pltx = 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 willbe the counts normalized to form a probability density, i.e.,``n/(len(x)*dbin)``.  In a probability density, the integral ofthe histogram should be 1; you can verify that with atrapezoidal 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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633599.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号