y 轴上的百分比

时间:2021-03-03 02:33:29

标签: python pandas matplotlib

使用这个列表作为数据集:

my list= [[],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     ['EMF'],
     ['body'],
     [],
     [],
     [],
     [],
     ['water', 'juice'],
     ['What', 'are', 'u', 'doing'],
     [],
     [],
     [],
     [],
     [],
     [],
     [],
     ['EVENT'],
     ['christmas'],
     [],
     ['shalala'],
     ['happy'],
     []]

我想绘制此数据在 y 轴上与总数相比的百分比。 我试过这个:

cnt = Counter(chain.from_iterable(df)) # df is the dataframe generated from the list above
plt.bar(*zip(*cnt.most_common(20)))
plt.xticks(rotation=60)
plt.show()

问题是我不知道百分比如何。希望能帮到你。

1 个答案:

答案 0 :(得分:3)

让我们试试value_counts

pd.Series(l).explode().dropna().\
      value_counts(normalize=True).sort_values(ascending=False).head(10).plot(kind='bar')