user3393007
user3393007

Reputation: 11

Combine Dic values less than x% to a single other slice piechart

Hello I have my datadict as below

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)

I would like to combine the dic values less than 10 % as Others and plot them in a pie chart using pyplot how can i combine the values less than 10% to others

Output pie graph should contain the results as below :

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

Thanks

Upvotes: 1

Views: 1408

Answers (1)

Corley Brigman
Corley Brigman

Reputation: 12401

You just need to filter pDict first, like this:

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)

Then everything else the same.

There might be a way to do this in one step, not sure, but that works.

Upvotes: 1

Related Questions