python数据集7个指标绘制正态分布检验图
时间: 2023-08-03 11:16:22 浏览: 164
好的,下面是 Python 绘制正态分布检验图的步骤:
1.首先,导入需要的库:
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from statsmodels.graphics.gofplots import qqplot
```
2.生成数据集,这里以随机生成一个大小为100的数据集为例:
```python
data = np.random.normal(size=100)
```
3.计算数据集的均值和标准差:
```python
mu, sigma = norm.fit(data)
```
4.绘制直方图并拟合正态分布:
```python
plt.hist(data, bins=10, density=True, alpha=0.6, color='g')
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, sigma)
plt.plot(x, p, 'k', linewidth=2)
plt.title("Histogram with fitted normal distribution")
plt.show()
```
5.绘制 Q-Q 图进行正态分布检验:
```python
qqplot(data, line='s')
plt.title("Q-Q plot for normality")
plt.show()
```
完整代码如下:
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from statsmodels.graphics.gofplots import qqplot
# 生成数据集
data = np.random.normal(size=100)
# 计算数据集的均值和标准差
mu, sigma = norm.fit(data)
# 绘制直方图并拟合正态分布
plt.hist(data, bins=10, density=True, alpha=0.6, color='g')
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, sigma)
plt.plot(x, p, 'k', linewidth=2)
plt.title("Histogram with fitted normal distribution")
plt.show()
# 绘制 Q-Q 图进行正态分布检验
qqplot(data, line='s')
plt.title("Q-Q plot for normality")
plt.show()
```
希望能对你有所帮助!
阅读全文