-2

Please, I want to know how I can plot a normal distribution plot.

Here is my code:

import numpy as np
import scipy.stats as stats
import pylab as pl

h=[27.3,27.6,27.5,27.6,27.3,27.6,27.9,27.5,27.4,27.5,27.5,27.4,27.1,27.0,27.3,27.4]

fit = stats.norm.pdf(h, np.mean(h), np.std(h))  #this is a fitting indeed

pl.plot(h,fit,'-o')
pl.hist(h,density=True)      #use this to draw histogram of your data
pl.show()                    #use may also need add this

I tried this but the curve is very rough.

Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43
  • Do you want to use `range(len(fit))` istead of `h` for the `x` value inside the `plot()` and `hist()`? – ilja Jun 21 '19 at 21:12

1 Answers1

1

Simply sort your list h.

Using sorted like this:

h = sorted([27.3,27.6,27.5,27.6,27.3,27.6,27.9,27.5,27.4,27.5,27.5,27.4,27.1,27.0,27.3,27.4])

Alternatively, you can also use h.sort().

h =[27.3,27.6,27.5,27.6,27.3,27.6,27.9,27.5,27.4,27.5,27.5,27.4,27.1,27.0,27.3,27.4]
h.sort()

Output: enter image description here

In order to get a smooth distribution curve, you can use seaborn.distplot():

import seaborn as sns
import scipy

h=[27.3,27.6,27.5,27.6,27.3,27.6,27.9,27.5,27.4,27.5,27.5,27.4,27.1,27.0,27.3,27.4]
ax = sns.distplot(h,fit=scipy.stats.norm, kde=False, hist=True, color='r')
ax.plot()

Output:

enter image description here

For more information on seaborn.distplot(), check this official documentation.

Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43