最近,我开始为数据科学学习Probability and Statistics.我试图为下面的分布X
绘制Standard Deviation,如68-95-99.7规则.
生成绘图的代码:
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
import seaborn as sns
# Line width: Maximum 130 characters in the output, post which it will continue in next line.
np.set_printoptions(linewidth=130)
sns.set_context("paper", font_scale=1.5)
# Distribution
X = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9]
mean = np.mean(X)
var = np.var(X)
std = np.std(X)
print("Mean:", mean)
print("Variance:", var)
print("Standard Deviation:", std)
"""
Mean: 5.0
Variance: 4.0
Standard Deviation: 2.0
"""
plt.figure(figsize=(10, 5))
ax = sns.kdeplot(X, shade=True)
# Plot 1-std
x = np.linspace(mean - std, mean + std)
y = norm.pdf(x, mean, std)
ax.fill_between(x, y, alpha=0.5)
plt.xlabel("Random variable X")
plt.ylabel("Probability Density Function")
plt.xticks(ticks=range(0, 10))
plt.grid()
plt.show()
This code is generating below plot:
问题:
- 从平均值绘制1 std的代码有什么错误?
- 我不明白为什么
kde
号地块上方会有一个小峰? - 如何绘制1-std、2-std和3-std?