我对 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. Ifstacked
is also True, the sum of the histograms is normalized to 1.Default is False
看起来很清楚。我见过它被称为密度函数、概率密度等。
也就是说,给定 [0, 10] 中大小为 1000 的随机均匀分布:
指定 normed=True
应该将 y 轴更改为密度轴,其中条形的总和为 1.0:
但实际上它没有做任何事情:
r = np.random.uniform(size=1000)
plt.hist(r, normed=True)
此外:
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/