我刚刚运行了一个简单的任务,试图为我运行的模拟绘制概率密度直方图。但是,当我绘制它时,每个 bin 的概率似乎与频率图的结果不匹配。对于 50 个 bin,我希望每个 bin 的平均概率为 2%,这在图表中没有反射(reflect)出来。

提前致谢

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plntAcres = 88.0
hvstPer = 0.99
hvstAcres = plntAcres*hvstPer
yldAcre = np.random.triangular(47,48,49, 10000)

carryIn = 464
pdn = hvstAcres * yldAcre
imp = 25.0
ttlSup = carryIn + pdn + imp

crush = np.random.uniform(1945, 1990,10000)
expts = np.random.uniform(2085, 2200,10000)
seedRes = 130
ttlDem = crush + expts + seedRes

carryOut = ttlSup - ttlDem

print carryOut

plt.hist(carryOut, bins=50,normed=True)
plt.title("Carry Out Distribution")
plt.xlabel("Value")
plt.ylabel("Probability")
plt.show()

Probability density of Carry out

最佳答案

hist 函数中,normed 参数不会产生概率,而是产生概率密度。如果您想要概率本身,请改用 weights 参数(并提供 1 / len(carryOut) )。

关键的两行:

weights = np.ones_like(carryOut) / (len(carryOut))
plt.hist(carryOut, bins=50, weights=weights)

关于python - Matplotlib 的概率密度直方图没有意义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42481698/

10-16 22:16