Skip to content

Instantly share code, notes, and snippets.

@ialexpovad
Created February 9, 2023 07:46
Show Gist options
  • Save ialexpovad/130ada56c66d975832d86084e3c5f256 to your computer and use it in GitHub Desktop.
Save ialexpovad/130ada56c66d975832d86084e3c5f256 to your computer and use it in GitHub Desktop.
Plotting a Gaussian normal curve with Python and Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
# define constants
mu = 998.8
sigma = 73.10
x1 = 900
x2 = 1100
# calculate the z-transform
z1 = ( x1 - mu ) / sigma
z2 = ( x2 - mu ) / sigma
x = np.arange(z1, z2, 0.001) # range of x in spec
x_all = np.arange(-10, 10, 0.001) # entire range of x, both in and out of spec
# mean = 0, stddev = 1, since Z-transform was calculated
y = norm.pdf(x,0,1)
y2 = norm.pdf(x_all,0,1)
# build the plot
fig, ax = plt.subplots(figsize=(9,6))
# plt.style.use('fivethirtyeight')
ax.plot(x_all,y2, 'k--', linewidth = 1)
ax.fill_between(x,y,0, alpha=0.3, color='b')
ax.fill_between(x_all,y2,0, alpha=0.1)
ax.set_xlim([-4,4])
# ax.set_xlabel('# of Standard Deviations Outside the Mean')
# ax.set_yticklabels([])
ax.set_title('Normal Gaussian Curve')
# plt.savefig('normal_curve.png', dpi=72, bbox_inches='tight')
# plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment