读取csv文件,获取前6的确诊人数数据,画柱形图并加上注释
时间: 2024-03-07 09:52:52 浏览: 60
好的,下面是读取csv文件,获取前6的确诊人数数据,画柱形图并加上注释的示例代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取csv文件
data = pd.read_csv('example.csv')
# 获取前6的确诊人数数据
top_6 = data.sort_values('confirmed', ascending=False)[:6]
# 绘制柱形图
plt.bar(top_6['province'], top_6['confirmed'])
# 添加注释
for i in range(len(top_6)):
plt.text(top_6.iloc[i]['province'], top_6.iloc[i]['confirmed'] + 100, str(top_6.iloc[i]['confirmed']), ha='center')
# 显示图像
plt.show()
```
在此示例中,我们使用`pandas`库读取csv文件,并使用`sort_values()`方法获取前6的确诊人数数据。然后,我们使用`plt.bar()`函数绘制柱形图,并使用`plt.text()`函数添加注释。最后,我们使用`plt.show()`函数显示图像。您可以根据需要调整注释的位置和格式。
阅读全文
相关推荐











