Given a mean and a variance is there a simple pylab function call which will plot a normal distribution?
Or do I need to make one myself?
Given a mean and a variance is there a simple pylab function call which will plot a normal distribution?
Or do I need to make one myself?
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 that in a single call. However you can find the Gaussian probability density function in scipy.stats
.
So the simplest way I could come up with is:
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 answer is correct. But becouse our mean can be more or less than zero I would still like to change this :
x = np.linspace(-3 * sigma, 3 * sigma, 100)
to this :
x = np.linspace(-3 * sigma + mean, 3 * sigma + mean, 100)
If you prefer to use a step by step approach you could consider a solution like follows
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()