将小于x%的Dic值合并到另一个切片饼图中

2024-05-14 22:02:49 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我有我的数据字典如下

pDict = {'"Streptococcous targeted"': 678000 , '"s.coccous file read"': 6780 , '"Mycobacterium Collection being analysed"': 80 , '"Mycobacterium Collection being analysed"': 8560 , '"S. viridans encountered "':450 , '"S. thermophilus Analysis intiated"':300 }

    labels = sorted(pDict.keys())
    sizes = pDict.values()
    k = len(pDict.keys())

    # Build Pie chart 
    list = plt.cm.gist_rainbow(np.linspace(0, 1, k))
    ax1 = fig.add_subplot(1,1,1,axisbg = 'grey')
    ax1.pie(sizes, labels = labels, colors = list,
        autopct='%1.1f%%', startangle=90)

我想将小于10%的dic值与其他值合并,并使用pyplot在饼图中绘制它们如何将小于10%的值组合到其他值

输出饼图应包含以下结果:

^{pr2}$

谢谢


Tags: 数据labels字典keyslistcollectionsizestargeted
1条回答
网友
1楼 · 发布于 2024-05-14 22:02:49

您只需先过滤pDict,如下所示:

psum = sum(pDict.values())
pthreshold = psum * 0.1

includedict = dict(k,v for k,v in pDict.items() if v >= pthreshold)
others = dict(k,v for k,v in pDict.items() if v < pthreshold)

其他一切都一样。在

也许有一种方法可以一步到位,但这是可行的。在

相关问题 更多 >

    热门问题