【发布时间】:2021-02-04 14:36:32
【问题描述】:
我可以在 python 中制作直方图,但无法添加密度曲线,我看到许多代码使用不同的方式在直方图上添加密度曲线,但我不确定如何使用我的代码
我添加了密度 = true 但无法在直方图上获得密度曲线
df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
X=df['A']
hist, bins = np.histogram(X, bins=10,density=True)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.show()
【问题讨论】:
-
您需要 seaborn 的
distplot()或histplot()。在最新版本 (0.11) 中,函数名称和参数发生了一些变化。请注意,np.histogram(..., density=True)表示直方图将被归一化,使得总面积总和为 1,因此它可以与 kdeplot 共享 y 轴。
标签: python pandas numpy matplotlib seaborn