栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

具有“所有其他类别”的Matplotlib饼图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具有“所有其他类别”的Matplotlib饼图

正如评论中已经提到的那样,最好的方法可能是在绘制之前进行操作。这是一种方法:

import pandas as pdfrom matplotlib import pyplot as pltimport numpy as npcountries = [        'Albania',        'Brazil',        'Denmark',        'France',        'Mexico',        'Nigeria',        'Spain',        'Germany',        'Finland',    ]#the full dataframedf = pd.Dataframe(    data = {'country': countries, 'value' :np.random.rand(len(countries))},    ).sort_values('value', ascending = False)#the top 5df2 = df[:5].copy()#othersnew_row = pd.Dataframe(data = {    'country' : ['others'],    'value' : [df['value'][5:].sum()]})#combining top 5 with othersdf2 = pd.concat([df2, new_row])#plotting -- for comparison left all countries and right #the others combinedfig, axes = plt.subplots(nrows = 1, ncols = 2, figsize = (9,4))df.plot(kind = 'pie', y = 'value', labels = df['country'], ax = axes[0])df2.plot(kind = 'pie', y = 'value', labels = df2['country'], ax = axes[1])axes[0].set_title('all countries')axes[1].set_title('top 5')plt.show()

结果看起来像这样。

希望这可以帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441171.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号