如何从 pandas DataFrame 创建饼图?

How to create a pie-chart from pandas DataFrame?

我有一个数据框,Count 按降序排列,看起来像这样:

df = pd.DataFrame({'Topic': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
                   'Count': [80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20]})

但超过 50 行。

我想为前 10 个主题创建一个饼图,其余主题汇总并在饼图中将其百分比表示为标签“Others”。是否可以排除每个饼图的饼图标签,并在图例中单独提及它们?

感谢期待

import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'Topic': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
                   'Count': [80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20]})
df.index = df.Topic
plot = df.plot.pie(y='Count', figsize=(5, 5))
plt.show()

使用文档:https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.pie.html

您需要制作一个新的数据框。假设您的计数按降序排序(如果不是,请使用 df.sort_values(by='Count', inplace=True)):

TOP = 10
df2 = df.iloc[:TOP]
df2 = df2.append({'Topic': 'Other', 'Count': df['Count'].iloc[TOP:].sum()},
                 ignore_index=True)

df2.set_index('Topic').plot.pie(y='Count', legend=False)

示例(N=10,N=5):

图例中的百分比:

N = 5
df2 = df.iloc[:N]
df2 = df2.append({'Topic': 'Other', 'Count': df['Count'].iloc[N:].sum()}, ignore_index=True)

df2.set_index('Topic').plot.pie(y='Count', legend=False)
leg = plt.legend(labels=df2['Count'])

输出:

如果 Series.where and then aggregate sum with Series.plot.pie 中没有顶部 N,则将 Topic 替换为 Other

N = 10
df['Topic'] = df['Topic'].where(df['Count'].isin(df['Count'].nlargest(N)), 'Other')

s = df.groupby('Topic')['Count'].sum()

pie = df.plot.pie(y='Count', legend=False)

#
labels = [f'{l}, {s:0.1f}%' for l, s in zip(s.index, s / s.sum())]
plt.legend(bbox_to_anchor=(0.85, 1), loc='upper left', labels=labels)