43

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

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))

output:

Standard Deriviation = 8.54065575872 
mean = 176.076923077

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

Saullo G. P. Castro
  • 49,101
  • 22
  • 160
  • 223
Adel
  • 2,606
  • 5
  • 27
  • 29
  • This is a duplicate of the older question at [python plot normal distribution](https://stackoverflow.com/questions/10138085/python-plot-normal-distribution) – Trenton McKinney Jun 27 '20 at 01:44

2 Answers2

95

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 

enter image description here

Trenton McKinney
  • 29,033
  • 18
  • 54
  • 66
Developer
  • 7,104
  • 7
  • 43
  • 53
  • 1
    normed has been deprecated and should now be replaced by density, but it works well: pl.hist(h,normed=True) – KateYoak Aug 06 '20 at 18:11
37

Assuming you're getting norm from scipy.stats, you probably just need to sort your list:

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

And so I get: enter image description here

Paul H
  • 52,530
  • 16
  • 137
  • 125