python - 使用 Matplotlib 绘制正态分布

标签 python numpy matplotlib plot scipy

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

数据:

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

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

最佳答案

  • 注意:此解决方案使用的是 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 

enter image description here

关于python - 使用 Matplotlib 绘制正态分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20011494/

相关文章:

python - ImportError : Imageio Pillow requires Pillow, 不是 PIL!在 M1 Mac 上

python - 将字符串计算为值

python - 将函数应用于多个列表 (Python)

python - Pandas Date Groupby 和 Apply - 性能改进

python - 错误 - 在 python 中计算 PCA 的欧几里得距离

python - 在不清除图形的情况下删除流图(matplotlib)

matplotlib - 将 matplotlib pcolormesh 输出保存到文件

python - 添加更多散点时,散点图会更改绘图输出

Python json 内存膨胀

python - numpy 数组的 set_printoptions 不适用于 numpy ndarray?