Python 用Matplotlib绘制正态分布图
请帮助我绘制以下数据的正态分布: 数据: 输出:Python 用Matplotlib绘制正态分布图,python,numpy,matplotlib,plot,scipy,Python,Numpy,Matplotlib,Plot,Scipy,请帮助我绘制以下数据的正态分布: 数据: 输出: Standard Deriviation = 8.54065575872 mean = 176.076923077 绘图不正确,我的代码有什么问题?假设您从scipy.stats获取norm,您可能只需要对列表进行排序: import numpy as np import scipy.stats as stats import matplotlib.pyplot as plt h = [186, 176, 158, 180, 186, 16
Standard Deriviation = 8.54065575872
mean = 176.076923077
绘图不正确,我的代码有什么问题?假设您从
scipy.stats
获取norm
,您可能只需要对列表进行排序:
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
h = [186, 176, 158, 180, 186, 168, 168, 164, 178, 170, 189, 195, 172,
187, 180, 186, 185, 168, 179, 178, 183, 179, 170, 175, 186, 159,
161, 178, 175, 185, 175, 162, 173, 172, 177, 175, 172, 177, 180]
h.sort()
hmean = np.mean(h)
hstd = np.std(h)
pdf = stats.norm.pdf(h, hmean, hstd)
plt.plot(h, pdf) # including h here is crucial
因此我得到:
注意:此解决方案使用的是pylab
,而不是matplotlib.pyplot
您可以尝试使用hist
将数据信息与拟合曲线放在一起,如下所示:
import numpy as np
import scipy.stats as stats
import pylab as pl
h = sorted([186, 176, 158, 180, 186, 168, 168, 164, 178, 170, 189, 195, 172,
187, 180, 186, 185, 168, 179, 178, 183, 179, 170, 175, 186, 159,
161, 178, 175, 185, 175, 162, 173, 172, 177, 175, 172, 177, 180]) #sorted
fit = stats.norm.pdf(h, np.mean(h), np.std(h)) #this is a fitting indeed
pl.plot(h,fit,'-o')
pl.hist(h,normed=True) #use this to draw histogram of your data
pl.show() #use may also need add this
这是一个重复的旧问题,在normed已被弃用,现在应该由密度取代,但它工作得很好:pl.hist(h,normed=True)
import numpy as np
import scipy.stats as stats
import pylab as pl
h = sorted([186, 176, 158, 180, 186, 168, 168, 164, 178, 170, 189, 195, 172,
187, 180, 186, 185, 168, 179, 178, 183, 179, 170, 175, 186, 159,
161, 178, 175, 185, 175, 162, 173, 172, 177, 175, 172, 177, 180]) #sorted
fit = stats.norm.pdf(h, np.mean(h), np.std(h)) #this is a fitting indeed
pl.plot(h,fit,'-o')
pl.hist(h,normed=True) #use this to draw histogram of your data
pl.show() #use may also need add this