我想创建一个显示国家/地区值的饼图。我有一个单列的 csv 文件,其中包含我读入 pandas 数据框的用户所在国家/地区的列表。 我在网上尝试了各种饼图教程,但无法绘制单列数据。
fig = plt.pyplot.figure()
ax = fig.add_subplot(111)
ax.hist(country)
数据示例:
country
0 BRAZIL
1 INDIA
2 INDIA
3 CHINA
4 RUSSIA
5 BRAZIL
最佳答案
您需要做的是在绘制之前计算每个国家出现的次数。试试这个:
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')
关于python - 无法在 pandas python 中绘制我的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25805082/