我希望在python中创建标准正态分布(mean = 0,Std Deviation = 1)曲线,然后在z-score(s)的左侧,右侧和中间创建阴影区域。我还想打印z-score(s)以及与阴影区域相关的概率。
比如说,我感兴趣的阴影区域是:
概率(z <-0.75)
概率(z> 0.75)
概率(-0.75 我使用以下行创建标准正态分布曲线。如何为相关的z分数添加阴影区域并打印z分数和概率?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()
答案 0 :(得分:3)
您必须使用绘制2条曲线之间区域的fill_between
函数,在这种情况下介于y = 0
和y = normal distribution
之间,以便于创建任务以下函数:
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()
示例:
x = np.arange(-3,3,0.001)
z0 = -0.75
draw_z_score(x, x<z0, 0, 1, 'z<-0.75')
输出:
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')
输出:
x = np.arange(-3,3,0.001)
z0 = 0.75
draw_z_score(x, x > z0, 0, 1, ' z> 0.75')
输出: