# How to plot a normal distribution with matplotlib in python ?

Published: February 09, 2019

Example of python code to plot a normal distribution with matplotlib:

````import matplotlib.pyplot as plt`
`import scipy.stats`
`import numpy as np`

`x_min = 0.0`
`x_max = 16.0`

`mean = 8.0 `
`std = 2.0`

`x = np.linspace(x_min, x_max, 100)`

`y = scipy.stats.norm.pdf(x,mean,std)`

`plt.plot(x,y, color='coral')`

`plt.grid()`

`plt.xlim(x_min,x_max)`
`plt.ylim(0,0.25)`

`plt.title('How to plot a normal distribution in python with matplotlib',fontsize=10)`

`plt.xlabel('x')`
`plt.ylabel('Normal Distribution')`

`plt.savefig("normal_distribution.png")`
`plt.show()`
```

Another example:

````import matplotlib.pyplot as plt`
`import scipy.stats`
`import numpy as np`

`x_min = 0.0`
`x_max = 16.0`

`mean = 8.0 `
`std = 3.0`

`x = np.linspace(x_min, x_max, 100)`

`y = scipy.stats.norm.pdf(x,mean,std)`

`plt.plot(x,y, color='black')`

`#----------------------------------------------------------------------------------------#`
`# fill area 1`

`pt1 = mean + std`
`plt.plot([pt1 ,pt1 ],[0.0,scipy.stats.norm.pdf(pt1 ,mean, std)], color='black')`

`pt2 = mean - std`
`plt.plot([pt2 ,pt2 ],[0.0,scipy.stats.norm.pdf(pt2 ,mean, std)], color='black')`

`ptx = np.linspace(pt1, pt2, 10)`
`pty = scipy.stats.norm.pdf(ptx,mean,std)`

`plt.fill_between(ptx, pty, color='#0b559f', alpha='1.0')`

`#----------------------------------------------------------------------------------------#`
`# fill area 2`

`pt1 = mean + std`
`plt.plot([pt1 ,pt1 ],[0.0,scipy.stats.norm.pdf(pt1 ,mean, std)], color='black')`

`pt2 = mean + 2.0 * std`
`plt.plot([pt2 ,pt2 ],[0.0,scipy.stats.norm.pdf(pt2 ,mean, std)], color='black')`

`ptx = np.linspace(pt1, pt2, 10)`
`pty = scipy.stats.norm.pdf(ptx,mean,std)`

`plt.fill_between(ptx, pty, color='#2b7bba', alpha='1.0')`

`#----------------------------------------------------------------------------------------#`
`# fill area 3`

`pt1 = mean - std`
`plt.plot([pt1 ,pt1 ],[0.0,scipy.stats.norm.pdf(pt1 ,mean, std)], color='black')`

`pt2 = mean - 2.0 * std`
`plt.plot([pt2 ,pt2 ],[0.0,scipy.stats.norm.pdf(pt2 ,mean, std)], color='black')`

`ptx = np.linspace(pt1, pt2, 10)`
`pty = scipy.stats.norm.pdf(ptx,mean,std)`

`plt.fill_between(ptx, pty, color='#2b7bba', alpha='1.0')`

`#----------------------------------------------------------------------------------------#`
`# fill area 4`

`pt1 = mean + 2.0 * std`
`plt.plot([pt1 ,pt1 ],[0.0,scipy.stats.norm.pdf(pt1 ,mean, std)], color='black')`

`pt2 = mean + 3.0 * std`
`plt.plot([pt2 ,pt2 ],[0.0,scipy.stats.norm.pdf(pt2 ,mean, std)], color='black')`

`ptx = np.linspace(pt1, pt2, 10)`
`pty = scipy.stats.norm.pdf(ptx,mean,std)`

`plt.fill_between(ptx, pty, color='#539ecd', alpha='1.0')`

`#----------------------------------------------------------------------------------------#`
`# fill area 5`

`pt1 = mean - 2.0 * std`
`plt.plot([pt1 ,pt1 ],[0.0,scipy.stats.norm.pdf(pt1 ,mean, std)], color='black')`

`pt2 = mean - 3.0 * std`
`plt.plot([pt2 ,pt2 ],[0.0,scipy.stats.norm.pdf(pt2 ,mean, std)], color='black')`

`ptx = np.linspace(pt1, pt2, 10)`
`pty = scipy.stats.norm.pdf(ptx,mean,std)`

`plt.fill_between(ptx, pty, color='#539ecd', alpha='1.0')`

`#----------------------------------------------------------------------------------------#`
`# fill area 6`

`pt1 = mean + 3.0 * std`
`plt.plot([pt1 ,pt1 ],[0.0,scipy.stats.norm.pdf(pt1 ,mean, std)], color='black')`

`pt2 = mean + 10.0 *std`
`plt.plot([pt2 ,pt2 ],[0.0,scipy.stats.norm.pdf(pt2 ,mean, std)], color='black')`

`ptx = np.linspace(pt1, pt2, 10)`
`pty = scipy.stats.norm.pdf(ptx,mean,std)`

`plt.fill_between(ptx, pty, color='#89bedc', alpha='1.0')`

`#----------------------------------------------------------------------------------------#`
`# fill area 7`

`pt1 = mean - 3.0 * std`
`plt.plot([pt1 ,pt1 ],[0.0,scipy.stats.norm.pdf(pt1 ,mean, std)], color='black')`

`pt2 = mean - 10.0 * std`
`plt.plot([pt2 ,pt2 ],[0.0,scipy.stats.norm.pdf(pt2 ,mean, std)], color='black')`

`ptx = np.linspace(pt1, pt2, 10)`
`pty = scipy.stats.norm.pdf(ptx,mean,std)`

`plt.fill_between(ptx, pty, color='#89bedc', alpha='1.0')`

`#----------------------------------------------------------------------------------------#`

`plt.grid()`

`plt.xlim(x_min,x_max)`
`plt.ylim(0,0.25)`

`plt.title('How to plot a normal distribution in python with matplotlib',fontsize=10)`

`plt.xlabel('x')`
`plt.ylabel('Normal Distribution')`

`plt.savefig("normal_distribution_2.png")`
`plt.show()`
```