import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
'''1、定义随机变量'''
mu=0
sigma=1
x=np.arange(-5,5,0.1)
x
array([-5.00000000e+00, -4.90000000e+00, -4.80000000e+00, -4.70000000e+00,
-4.60000000e+00, -4.50000000e+00, -4.40000000e+00, -4.30000000e+00,
-4.20000000e+00, -4.10000000e+00, -4.00000000e+00, -3.90000000e+00,
-3.80000000e+00, -3.70000000e+00, -3.60000000e+00, -3.50000000e+00,
-3.40000000e+00, -3.30000000e+00, -3.20000000e+00, -3.10000000e+00,
-3.00000000e+00, -2.90000000e+00, -2.80000000e+00, -2.70000000e+00,
-2.60000000e+00, -2.50000000e+00, -2.40000000e+00, -2.30000000e+00,
-2.20000000e+00, -2.10000000e+00, -2.00000000e+00, -1.90000000e+00,
-1.80000000e+00, -1.70000000e+00, -1.60000000e+00, -1.50000000e+00,
-1.40000000e+00, -1.30000000e+00, -1.20000000e+00, -1.10000000e+00,
-1.00000000e+00, -9.00000000e-01, -8.00000000e-01, -7.00000000e-01,
-6.00000000e-01, -5.00000000e-01, -4.00000000e-01, -3.00000000e-01,
-2.00000000e-01, -1.00000000e-01, -1.77635684e-14, 1.00000000e-01,
2.00000000e-01, 3.00000000e-01, 4.00000000e-01, 5.00000000e-01,
6.00000000e-01, 7.00000000e-01, 8.00000000e-01, 9.00000000e-01,
1.00000000e+00, 1.10000000e+00, 1.20000000e+00, 1.30000000e+00,
1.40000000e+00, 1.50000000e+00, 1.60000000e+00, 1.70000000e+00,
1.80000000e+00, 1.90000000e+00, 2.00000000e+00, 2.10000000e+00,
2.20000000e+00, 2.30000000e+00, 2.40000000e+00, 2.50000000e+00,
2.60000000e+00, 2.70000000e+00, 2.80000000e+00, 2.90000000e+00,
3.00000000e+00, 3.10000000e+00, 3.20000000e+00, 3.30000000e+00,
3.40000000e+00, 3.50000000e+00, 3.60000000e+00, 3.70000000e+00,
3.80000000e+00, 3.90000000e+00, 4.00000000e+00, 4.10000000e+00,
4.20000000e+00, 4.30000000e+00, 4.40000000e+00, 4.50000000e+00,
4.60000000e+00, 4.70000000e+00, 4.80000000e+00, 4.90000000e+00])
'''2、概率密度函数PDF'''
y=stats.norm.pdf(x,mu,sigma)
'''3、绘图'''
fig=plt.figure()
plt.plot(x,y)
plt.xlabel('随机变量:x')
plt.ylabel('概率:y')
plt.title('正态分布:$\mu$=%.1f,$\sigma^2$=%.1f' % (mu,sigma))
plt.show()
|