我看过很多关于python中正态分布和曲线草图的文档,对此我有些困惑,我已经生成了均值为30和标准差为3.7的正态随机变量,并使用了norm.dist函数,我估计了pdf功能
=NORM.DIST(A2,$H$2,$I$2,FALSE)
我想使用python进行演示,我发现了scipy和numpy版本,请帮助我清楚地说明问题,这是我的一些数字
我已经尝试过以下代码
from scipy.stats import norm
import pandas as pd
import matplotlib.pyplot as plt
data_random =pd.read_excel("data_for_normal.xlsx")
data_values =data_random["NormalVariables"].values
pdf_values =norm.pdf(data_values,30,3.7)
plt.plot(data_values,pdf_values)
plt.title("normal curve")
plt.xlabel("x values")
plt.ylabel("probability density function")
plt.show()
结果:
print(data_random.head(10))
NormalVariables
0 29.214494
1 30.170595
2 36.014144
3 30.388626
4 28.398749
5 24.861042
6 29.519316
7 24.207164
8 35.779376
9 26.042977
# plt.plot connects datapoints with lines:
x = [0,1,2]
y = [1,4,3]
plt.plot(x,y)
#note that lines are drawn between adjacent elements in the list,
#so a line from (0,1) to (1,4) and then to (2,3)
# if the order of the datapoints is changed, the position of the datapoints
# remains unchanged, but now lines are drawn between different points
x = [2,0,1]
y = [3,1,4]
plt.plot(x,y)
所以您在图中看到所有交叉的原因是您绘制了未排序的数据。
如果只想从excel复制绘图,请改用plt.scatter
。此图仅绘制数据点,而未绘制它们之间的连接。
x = [2,0,1]
y = [3,1,4]
plt.scatter(x,y)