matplotlib piechart show small groups as others

Solutions on MaxInterview for matplotlib piechart show small groups as others by the best coders in the world

showing results for - "matplotlib piechart show small groups as others"
Mathieu
05 Jun 2018
1import matplotlib.pyplot as plt
2plt.style.use('ggplot')
3dic = {'Class a': 26.9, 'Class b': 18, 'Class c': 16.8, 'Class d': 13,
4       'Class e': 8.83,'Class f': 5.97,'Class g': 3.59,'Class h': 2.01,
5       'Class i': 1.42,'Class j': 1.09,'Class k': 0.903,'Class l': 0.873,
6       'Class m': 0.28,'Class n': 0.24,'Class o': 0.112}
7
8# group together all elements in the dictionary whose value is less than 2
9# name this group 'All the rest'
10import itertools
11newdic={}
12for key, group in itertools.groupby(dic, lambda k: 'All the rest' if (dic[k]<2) else k):
13     newdic[key] = sum([dic[k] for k in list(group)])   
14labels = newdic.keys()
15sizes = newdic.values()
16fig, ax = plt.subplots()
17ax.pie(sizes, labels=labels, autopct='%1.1f%%', explode=(0,0,0,0,0,0,0,0,.1), startangle=0)
18ax.axis('equal')
19plt.tight_layout()
20plt.show()
similar questions