pandas正太分布图
时间: 2024-06-07 12:04:52 浏览: 31
在Python的数据分析库Pandas中,绘制正态分布图(也称为高斯分布图)通常用于可视化数据的分布情况,特别是在统计学中,正态分布是非常常见的一种假设。Pandas本身并不直接提供绘制正态分布图的功能,但你可以借助其他如Matplotlib或Seaborn库来完成这个任务。
如果你想要用Pandas配合matplotlib来展示数据的正态分布,你可以按照以下步骤操作:
1. **加载数据和库**:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
```
2. **生成随机数据**:
```python
data = pd.Series(norm.rvs(size=1000)) # 使用norm.rvs()函数生成正态分布的数据
```
3. **绘制正态分布图**:
```python
fig, ax = plt.subplots()
ax.hist(data, bins='auto', density=True) # 'density=True' 让直方图变为累积频率,便于比较正态曲线
mu, sigma = norm.fit(data) # 获取数据的平均值(μ)和标准差(σ)
x = np.linspace(min(data), max(data), 100)
y = norm.pdf(x, mu, sigma) # 正态分布概率密度函数
ax.plot(x, y, 'r-', linewidth=2, label='Fit results') # 绘制正态曲线
ax.legend()
plt.title('Histogram with Normal Distribution')
plt.show()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)