无法在pandas中绘制饼图
fig = plt.pyplot.figure()
ax = fig.add_subplot(111)
ax.hist(country)
我想制作一个饼图,来展示各个国家的数值。我有一个单列的csv文件,里面列出了用户来自哪些国家,我把这个文件读入了一个pandas数据框中。
我在网上尝试了各种饼图的教程,但就是无法用这单列的数据来绘制饼图。
数据示例:
country
0 BRAZIL
1 INDIA
2 INDIA
3 CHINA
4 RUSSIA
5 BRAZIL
1 个回答
19
你需要先统计每个国家出现的次数,然后再进行绘图。可以试试这个方法:
import pandas as pd
import matplotlib.pyplot as plt
#import your data here
#Plot a histogram of frequencies
df.country.value_counts().plot(kind='barh')
plt.title('Number of appearances in dataset')
plt.xlabel('Frequency')
#Now make a pie chart
df.country.value_counts().plot(kind='pie')
plt.axis('equal')
plt.title('Number of appearances in dataset')