Given the mean and variance, is there a simple pylab function call that will build a normal distribution?
Or do I need to do this on my own?
import matplotlib.pyplot as plt import numpy as np import matplotlib.mlab as mlab import math mu = 0 variance = 1 sigma = math.sqrt(variance) x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100) plt.plot(x,mlab.normpdf(x, mu, sigma)) plt.show()
I donβt think there is a function that does all this in one call. However, you can find the Gaussian probability density function in scipy.stats .
scipy.stats
So the easiest way:
import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm # Plot between -10 and 10 with .001 steps. x_axis = np.arange(-10, 10, 0.001) # Mean = 0, SD = 2. plt.plot(x_axis, norm.pdf(x_axis,0,2))
Sources:
Unutbu's answer is correct. But since our average value can be more or less than zero, I would still like to change this:
x = np.linspace(-3,3,100)
:
x = np.linspace(-3+mean,3+mean,100)
If you prefer a step-by-step approach, you might consider a solution similar to the following
import numpy as np import matplotlib.pyplot as plt mean = 0; std = 1; variance = np.square(std) x = np.arange(-5,5,.01) f = np.exp(-np.square(x-mean)/2*variance)/(np.sqrt(2*np.pi*variance)) plt.plot(x,f) plt.ylabel('gaussian distribution') plt.show()