由于我正在使用大型数据集,因此我想找到一种标记数据的快捷方式。
这是我从大型数据集中绘制的数据:
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()
通过这种方式可以轻松快捷地创建饼图,但是我还没有弄清楚如何在饼图中自动标记每个国家而不必一一标记每个数据点。
您应该使用pandas
绘图功能:
# count:
Nations = the_data['Nationality'].value_counts()
# plot data
Nations.plot.pie()
plt.title('Percentage of Players from Each Country')
plt.show()