导入年龄csv文件,绘制年龄分布直方图并加上边框和数据
时间: 2024-03-10 12:50:34 浏览: 26
好的,以下是绘制年龄分布直方图并加上边框和数据的示例代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 导入CSV文件
df = pd.read_csv('age.csv')
# 绘制直方图
plt.hist(df['age'], bins=20)
# 添加标题和坐标轴标签
plt.title('Age Distribution')
plt.xlabel('Age')
plt.ylabel('Frequency')
# 添加边框和数据
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['bottom'].set_linewidth(0.5)
plt.gca().spines['left'].set_linewidth(0.5)
plt.text(0.8, 0.9, 'Mean: {:.2f}'.format(df['age'].mean()), transform=plt.gca().transAxes)
plt.text(0.8, 0.85, 'Median: {}'.format(df['age'].median()), transform=plt.gca().transAxes)
plt.text(0.8, 0.8, 'Std: {:.2f}'.format(df['age'].std()), transform=plt.gca().transAxes)
# 显示图形
plt.show()
```
请注意,这只是示例代码,您需要将代码中的“age.csv”替换为您自己的CSV文件名,并根据需要进行修改。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)