Python pylab plot normal distribution

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?

+46
python matplotlib
Apr 13 2018-12-12T00: 00Z
source share
4 answers
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() 

gass ​​distro, mean is 0 variance 1

+89
Apr 13 2018-12-12T00:
source share

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 .

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:

+27
Apr 13 2018-12-12T00:
source share

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) 
+5
07 Feb '15 at 10:21
source share

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() 
0
Dec 11 '17 at 23:18
source share



All Articles