本文介绍了使用 Matplotlib 绘制正态分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我绘制以下数据的正态分布:

please help me to plot the normal distribution of the folowing data:

数据:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

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]

std = np.std(h)
mean = np.mean(h)
plt.plot(norm.pdf(h,mean,std))

输出:

Standard Deriviation = 8.54065575872
mean = 176.076923077

情节不正确,我的代码有什么问题?

the plot is incorrect, what is wrong with my code?

推荐答案

注意:此解决方案使用的是 pylab,而不是 matplotlib.pyplot

您可以尝试使用 hist 将您的数据信息与拟合曲线一起放置,如下所示:

Note: This solution is using pylab, not matplotlib.pyplot

You may try using hist to put your data info along with the fitted curve as below:

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

这篇关于使用 Matplotlib 绘制正态分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 12:58