Search code examples
pythonmatplotlibdistributiongaussian

What's the correct usage of matplotlib.mlab.normpdf()?


I intend for part of a program I'm writing to automatically generate Gaussian distributions of various statistics over multiple raw text sources, however I'm having some issues generating the graphs as per the guide at:

python pylab plot normal distribution

The general gist of the plot code is as follows.

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as pyplot

meanAverage = 222.89219487179491    # typical value calculated beforehand
standardDeviation = 3.8857889432054091    # typical value calculated beforehand

x = np.linspace(-3,3,100)
pyplot.plot(x,mlab.normpdf(x,meanAverage,standardDeviation))
pyplot.show()

All it does is produce a rather flat looking and useless y = 0 line! Can anyone see what the problem is here?

Cheers.


Solution

  • It looks like you made a few small but significant errors. You either are choosing your x vector wrong or you swapped your stddev and mean. Since your mean is at 222, you probably want your x vector in this area, maybe something like 150 to 300. This way you get all the good stuff, right now you are looking at -3 to 3 which is at the tail of the distribution. Hope that helps.