如何用numpy正确绘制直方图,并与密度函数进行匹配?

问题描述 投票:0回答:1

TL;DR:如何使用 Numpy 正确绘制

np.histogram(..., density=True)
的结果?

使用

density=True
应该有助于匹配样本的直方图和基础随机变量的密度函数,但事实并非如此:

import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
y = np.random.randn(10000)
h, bins = np.histogram(y, bins=1000, density=True)
plt.bar(bins[:-1], h)
x = np.linspace(-10, 10, 100)
f = scipy.stats.norm.pdf(x)
plt.plot(x, f, color="green")
plt.show()

为什么直方图和概率密度函数没有相应缩放?

在这种情况下,观察表明 1.6 缩放会更好:

plt.plot(x, 1.6 * f, color="green")

此外,这也可以正常工作:

plt.hist(y, bins=100, density=True)

为什么?

python numpy statistics probability probability-density
1个回答
1
投票

使用自动垃圾箱怎么样?

h, bins = np.histogram(y, bins='auto', density=True)
© www.soinside.com 2019 - 2024. All rights reserved.