I am looking to create a standard normal distribution (mean=0, Std Deviation=1) curve in python and then shade area to the left, right and the middle of z-score(s). I also want to print the z-score(s) and the associated probability with the shaded area.
Say for example, the shaded areas I am interested in are:
Probability(z < -0.75)
Probability(z > 0.75)
Probability(-0.75 < z < 0.75)
I used the following lines to create the standard normal distribution curve. How can I add the shaded region for associated z-scores and print the z-scores along with the probabilities?
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
range = np.arange(-3,3,0.001)
plt.plot(range, norm.pdf(range, 0, 1))
plt.show()
You must use the fill_between
function that draws the area between 2 curves, in this case between y = 0
and y = normal distribution
, to facilitate the task has been created the following function:
def draw_z_score(x, cond, mu, sigma, title):
y = norm.pdf(x, mu, sigma)
z = x[cond]
plt.plot(x, y)
plt.fill_between(z, 0, norm.pdf(z, mu, sigma))
plt.title(title)
plt.show()
Example:
x = np.arange(-3,3,0.001)
z0 = -0.75
draw_z_score(x, x<z0, 0, 1, 'z<-0.75')
Output:
x = np.arange(-3,3,0.001)
z0 = 0.75
draw_z_score(x, (-z0 < x) & (x < z0), 0, 1, '-0.75<z<0.75')
Output:
x = np.arange(-3,3,0.001)
z0 = 0.75
draw_z_score(x, x > z0, 0, 1, ' z> 0.75')
Output:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With