准备数据data,存成int型list,或float型list
导入模块
#!/usr/bin env python3.6
#-*-coding=utf-8-*-
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import kstest
from scipy.stats import shapiro
from scipy.stats import norm
import seaborn as sns
正态分布检验的两种方法
data = open(datafile).read().strip().split("\n")
data = [ float(x) for x in data ]
data = np.array(data)
value, p = kstest(data, 'norm')
print(value, p)
value, p = shapiro(data)
print(value, p)
绘制直方图+正态分布曲线(PDF函数)
mu =np.mean(data) #计算均值
sigma =np.std(data)
num_bins = 30 #直方图柱子的数量
fig,ax=plt.subplots()
n,bins,patches=ax.hist(data,num_bins,density=1,color='lightgreen')
y=norm.pdf(bins,mu,sigma)
ax.plot(bins,y,'--')
ax.axvline(mu,color='r',linewidth=0.5)
ax.axvline(mu-3*sigma,color='r',linewidth=0.5)
ax.axvline(mu+3*sigma,color='r',linewidth=0.5)
ax.set_xlabel('this is x lab')
ax.set_ylabel('this is y lab')
ax.set_title(r'this is title')
fig.tight_layout()
plt.show()
绘制直方图+拟合PDF函数
sns.set_palette("hls") #设置所有图的颜色,使用hls色彩空间
sns.distplot(data,color="r",bins=30,kde=True)
plt.show()