我有以下正態分布圖
x_axis = np.arange(-10, 10, 0.001)
pdf = stats.norm.pdf(x_axis, np.mean(x_axis), np.std(x_axis))
plt.plot(x_axis, pdf)
我想陰影/突出顯示正態分布下的 95% 置信區間。我還想在 x 軸上標記 95% 的上限和下限值。
非常感謝幫助!
uj5u.com熱心網友回復:
您可以使用plt.fill_between.
我在這里使用了標準正態分布 (0,1),因為您的計算x_axis會使顯示范圍太窄而無法看到填充。
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
x_axis = np.arange(-10, 10, 0.001)
avg = 0
std = 1
pdf = stats.norm.pdf(x_axis, avg, std)
plt.plot(x_axis, pdf)
std_lim = 1.96 # 95% CI
low = avg-std_lim*std
high = avg std_lim*std
plt.fill_between(x_axis, pdf, where=(low < x_axis) & (x_axis < high))
plt.text(low, 0, low, ha='center')
plt.text(high, 0, high, ha='center')

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/326472.html
標籤:Python matplotlib 图形 分配 正态分布
