无法在 Pandas python 中绘制我的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25805082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to pie-plot my data in pandas python
提问by jmz
I want to create a pie-plot that will show country values. I have a single column csv file that has list of countries where the users are from that I read into a pandas dataframe. I've tried all sorts of pie-plot tutorials on web but was unable to plot this single column data.
我想创建一个显示国家/地区值的饼图。我有一个单列 csv 文件,其中包含我读入 Pandas 数据框的用户所在国家/地区的列表。我在网上尝试了各种饼图教程,但无法绘制这个单列数据。
fig = plt.pyplot.figure()
ax = fig.add_subplot(111)
ax.hist(country)
Data example:
数据示例:
country
0 BRAZIL
1 INDIA
2 INDIA
3 CHINA
4 RUSSIA
5 BRAZIL
回答by jmz
What you need to do is count the number of times each country appears before you plot it. Try this:
您需要做的是在绘制之前计算每个国家/地区出现的次数。尝试这个:
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')



