Matplotlib 的概率密度直方图没有意义
probability density histogram with Matplotlib doesnt make sense
我刚刚 运行 尝试绘制模拟 I 运行 的概率密度直方图的简单任务。但是,当我绘制它时,每个 bin 的概率似乎与频率图的结果不匹配。对于 50 个 bin,我希望每个 bin 的平均概率为 2%,这没有反映在图表中。
提前致谢
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
您的模式是钟形曲线,通常意味着您的随机变量呈正态分布。
检查维基百科 Normal Distribution / Gauss distribution
在hist
函数中,normed
参数不会产生概率,而是产生概率密度。如果您想要概率本身,请改用 weights
参数(并提供 1 / len(carryOut)
)。
关键的两行:
weights = np.ones_like(carryOut) / (len(carryOut))
plt.hist(carryOut, bins=50, weights=weights)
我刚刚 运行 尝试绘制模拟 I 运行 的概率密度直方图的简单任务。但是,当我绘制它时,每个 bin 的概率似乎与频率图的结果不匹配。对于 50 个 bin,我希望每个 bin 的平均概率为 2%,这没有反映在图表中。
提前致谢
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
您的模式是钟形曲线,通常意味着您的随机变量呈正态分布。 检查维基百科 Normal Distribution / Gauss distribution
在hist
函数中,normed
参数不会产生概率,而是产生概率密度。如果您想要概率本身,请改用 weights
参数(并提供 1 / len(carryOut)
)。
关键的两行:
weights = np.ones_like(carryOut) / (len(carryOut))
plt.hist(carryOut, bins=50, weights=weights)