2
$\begingroup$

When plotting a Pie chart from a dictionary using Python's matplotlib, I get some classes that are too mashed together because of their small size. Is there a way to group the smallest values together and maybe plot them in a separate pie chart (or graduated bar) using python?

Here is the code I used:

import matplotlib.pyplot as plt

plt.style.use('ggplot')

dic = {'class a': 26.9,
       'Class b': 18,
       'Class c': 16.8,
       'Class d': 13,
       'Class e': 8.83,
       'Class f': 5.97,
       'Class g': 3.59,
       'Class h': 2.01,
       'Class i': 1.42,
       'Class j': 1.09,
       'Class k': 0.903,
       'Class l': 0.873,
       'Class m': 0.28,
       'Class n': 0.24,
       'Class o': 0.112}

labels = dic.keys()
sizes = dic.values()

fig, ax = plt.subplots()

ax.pie(sizes, labels=labels, autopct='%1.1f%%')
ax.axis('equal')
plt.tight_layout()
plt.show()
'''
$\endgroup$
7
  • $\begingroup$ If I understand your question right, you want to group the classes which ever are less than certain percentage and show it in a separate pie chart? $\endgroup$
    – Toros91
    Aug 20, 2019 at 9:04
  • $\begingroup$ @Toros91that's exactly it. $\endgroup$ Aug 20, 2019 at 9:04
  • $\begingroup$ you can use a threshold and separate them into 2 data-frames. Plot them separately $\endgroup$
    – Toros91
    Aug 20, 2019 at 9:06
  • $\begingroup$ @Toros91 Can you please share how to that? $\endgroup$ Aug 20, 2019 at 9:07
  • $\begingroup$ Do you have this dictionary in the form of a dataframe? $\endgroup$
    – Toros91
    Aug 20, 2019 at 9:19

2 Answers 2

2
$\begingroup$

An idea would be to group the smaller slices together by grouping all the classes whose value is beneath a certain threshold, let's say in this case 2, and summing up their values.

Here's an example:

import matplotlib.pyplot as plt

plt.style.use('ggplot')

dic = {'Class a': 26.9,
       'Class b': 18,
       'Class c': 16.8,
       'Class d': 13,
       'Class e': 8.83,
       'Class f': 5.97,
       'Class g': 3.59,
       'Class h': 2.01,
       'Class i': 1.42,
       'Class j': 1.09,
       'Class k': 0.903,
       'Class l': 0.873,
       'Class m': 0.28,
       'Class n': 0.24,
       'Class o': 0.112}

# group together all elements in the dictionary whose value is less than 2
# name this group 'All the rest'
import itertools
newdic={}
for key, group in itertools.groupby(dic, lambda k: 'All the rest' if (dic[k]<2) else k):
     newdic[key] = sum([dic[k] for k in list(group)])   

labels = newdic.keys()
sizes = newdic.values()

fig, ax = plt.subplots()

ax.pie(sizes, labels=labels, autopct='%1.1f%%', explode=(0,0,0,0,0,0,0,0,.1), startangle=0)
ax.axis('equal')
plt.tight_layout()

plt.show()

Group smaller slices in pie chart

If it is important to show the values for the smaller slices, just plot a table with the missing data to the side.

$\endgroup$
1
$\begingroup$

This might not be the best solution:

I'm attaching the new code in which I split the data using a threshold and then plot them separately into 2 pie charts:

import matplotlib.pyplot as plt
import pandas as pd

plt.style.use('ggplot')

dic = [{'class a': 26.9,
       'Class b': 18,
       'Class c': 16.8,
       'Class d': 13,
       'Class e': 8.83,
       'Class f': 5.97,
       'Class g': 3.59,
       'Class h': 2.01,
       'Class i': 1.42,
       'Class j': 1.09,
       'Class k': 0.903,
       'Class l': 0.873,
       'Class m': 0.28,
       'Class n': 0.24,
       'Class o': 0.112}]

labels = dic.keys()
sizes = dic.values()

#converting the data into a dataframe
df = pd.DataFrame(dic)
dft = df.transpose()

dft.columns = ['Value']
dft['Class'] = dft.index

#defining the threshold 
threshold = 5
dft_subset1 =  dft[dft['Value'] <= threshold]
dft_subset2 =  dft[dft['Value'] > threshold]

plot1 = dft_subset1.plot.pie(y='Value', figsize=(5, 5))
plot2 = dft_subset2.plot.pie(y='Value', figsize=(5, 5))

Output:

Output

$\endgroup$
2
  • $\begingroup$ Is it possible to move the pie chart legend out of the circle? $\endgroup$
    – martin
    Jul 25, 2020 at 21:28
  • $\begingroup$ Never tried it, will need to do some research. $\endgroup$
    – Toros91
    Jul 26, 2020 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.