Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 686
James Paul Turner Avatar asked Oct 13 '12 23:10

James Paul Turner


People also ask

What is mlab in python?

Mlab is a high-level python to Matlab bridge that lets Matlab look like a normal python library. This python library is based on the work of original mlabwrap project. http://mlabwrap.sourceforge.net/


2 Answers

If you read documentation of matplotlib.mlab.normpdf, this function is deprycated and you should use scipy.stats.norm.pdf instead.

Deprecated since version 2.2: scipy.stats.norm.pdf

And because your distribution mean is about 222, you should use np.linspace(200, 220, 100).

So your code will look like:

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

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

x = np.linspace(200, 220, 100)
pyplot.plot(x, norm.pdf(x, meanAverage, standardDeviation))
pyplot.show()
like image 132
733amir Avatar answered Oct 12 '22 22:10

733amir


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.

like image 43
dvreed77 Avatar answered Oct 12 '22 22:10

dvreed77