5

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.

Community
  • 1
  • 1
James Paul Turner
  • 791
  • 3
  • 8
  • 23

3 Answers3

18

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()
733amir
  • 333
  • 2
  • 6
8

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.

dvreed77
  • 2,217
  • 2
  • 27
  • 42
0

I see that, for the *args which are sending meanAverage, standardDeviation, the correct thing to be sent is:

mu : a numdims array of means of a

sigma : a numdims array of atandard deviation of a

Does this help?

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131