python - 无法在 pandas python 中绘制我的数据

标签 python matplotlib pandas

我想创建一个显示国家/地区值的饼图。我有一个单列的 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')

enter image description here

#Now make a pie chart
df.country.value_counts().plot(kind='pie')
plt.axis('equal')
plt.title('Number of appearances in dataset')

enter image description here

关于python - 无法在 pandas python 中绘制我的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25805082/

相关文章:

python - Numpy 列和行向量

python - Keras 预测形状不正确?

python - 如何将分组条形图从垂直转换为水平

Python basemap 立体图

python - 在 matplotlib 散点图中突出显示特定点

python - 分解两个 PySpark 数组并保留相同位置的元素

python - 如果值位于另一个数据帧中,如何从一个数据帧获取行

python - time.strptime 函数在线程模式下抛出错误

python - pickle 剥削

python - 根据字符串值列对 pandas 数据框行进行排序