Python饼图/显示多列组合

问题描述 投票:0回答:1

我有2列的数据框:

Col1-经理的姓名

Col2-他们的利润

[我想绘制一个饼图,在其中我可以分别显示利润最高的5位经理,而其他人则一人表示为'其他'

pandas pie-chart
1个回答
0
投票
怎么样:使用autopct参数自动标记饼图。

import pandas as pd import matplotlib.pyplot as plt data = {'managers':['mike1','mike2','mike3','mike4','mike5','mike6','mike7'], 'profit':[110,60,40,30,10,5,5], } df = pd.DataFrame(data) df = df.sort_values(by = 'profit', ascending = False) top_5 = df.iloc[:5] others = df.iloc[5:]['profit'].sum() df2 = pd.DataFrame([['others',others]], columns = ['managers','profit']) all_data = top_5.append(df2, ignore_index=True) all_data.index = all_data['managers'] #func to lable the pieces def auto_func(val): return(round(val)) all_data.plot.pie(y = 'profit', autopct = auto_func) # ax = plt.gca() plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.