python - 为什么 matplotlib 直方图的 `normed` 参数没有任何作用?

标签 python matplotlib

我对 matplotlib.pyplot.hist 中的 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), i.e., the integral of the histogram will sum to 1. If stacked is also True, the sum of the histograms is normalized to 1.

Default is False

看起来很清楚。我见过它被称为密度函数、概率密度等。

也就是说,给定 [0, 10] 中大小为 1000 的随机均匀分布:

enter image description here

指定 normed=True 应该将 y 轴更改为密度轴,其中条形的总和为 1.0:

enter image description here

但实际上它没有做任何事情:

r = np.random.uniform(size=1000)
plt.hist(r, normed=True)

enter image description here

此外:

print(plt.hist(r, normed=True)[0].sum())
# definitely not 1.0
10.012123595

所以,我看到了 @Carsten König 的 answers类似的问题,我不要求解决方法。我的问题是,normed 的目的是什么?我是否误解了这个参数的实际作用?

matplotlib documentation甚至给出了一个名为“histogram_percent_demo”的示例,其中积分看起来将超过百分之一千。

最佳答案

条形的高度之和不一定为 1。 就是曲线下的面积,和直方图的积分一样,等于一:

import numpy as np
import matplotlib.pyplot as plt
r = np.random.uniform(size=1000)
hist, bins, patches = plt.hist(r, normed=True)

print((hist * np.diff(bins)).sum())
# 1.0

norm=True 因此返回一个可以解释为概率分布的直方图。

关于python - 为什么 matplotlib 直方图的 `normed` 参数没有任何作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45888179/

相关文章:

python - 如何在 SQLAlchemy 中将 joinedload/contains_eager 用于启用查询的关系(惰性 ='dynamic' 选项)

python - 是否可以使用 Matplotlib 绘制隐式方程?

python - 在 Win32 上如何/在哪里下载适用于 Python 2.7 的 Pylab?

python - 在 IPython 中抑制 Matplotlib 图例警告

python - 通过API调用xbacklight

python - 类中的方法如何访问类常量变量?

python - 有没有办法在Windows 7中不使用QT编译wireshark解析器插件?

python - 列表中列表的乘积显示为列表

python - 无论如何要在 python 中快速绘制多条线?如果可能,首选 Matplotlib

python - Pandas 绘制 4 个数据帧系列中的 2 个子图