将小于x%的Dict值组合到单个其他切片饼图中

时间:2014-03-07 14:43:17

标签: python matplotlib

您好我的datadict如下

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%的值组合到其他

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

'"Streptococcous targeted"': 678000 , '"s.coccous file read"': 6780 ,  '"Mycobacterium Collection being analysed"': 8560 , '"others" :800 

谢谢

1 个答案:

答案 0 :(得分:1)

您只需首先过滤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)

然后其他一切都一样。

可能有一种方法可以一步完成,但不确定,但是有效。