首页 > 解决方案 > 如何在 matplotlib 中自动标记数据?

问题描述

由于我正在处理大型数据集,因此我想找到标记数据的快捷方式。

这是我从大型数据集中绘制的数据:

Nationality
Afghanistan      4
Albania         40
Algeria         60
Andorra          1
Angola          15
              ...
Uzbekistan       2
Venezuela       67
Wales          129
Zambia           9
Zimbabwe        13
Name: count, Length: 164, dtype: int64

到目前为止,这是我的代码:

import pandas as pd 
import matplotlib.pyplot as plt 

the_data = pd.read_csv('fifa_data.csv')

plt.title('Percentage of Players from Each Country')

the_data['count'] = 1
Nations = the_data.groupby(['Nationality']).count()['count']

plt.pie(Nations)
plt.show()

通过这种方式创建饼图既简单又快捷,但我还没有弄清楚如何在饼图中自动标记每个国家,而不必一一标记每个数据点。

标签: pythonmatplotliblabelpie-chart

解决方案


pandas绘图功能会自动为您标记数据

# count:
Nations = the_data.groupby('Nationality').size()

# plot data
Nations.plot.pie()

plt.title('Percentage of Players from Each Country')
plt.show()

推荐阅读