0

wanted to draw two pie charts beside each other, but the text from right figure goes iside let fig. I tried subplots_adjust, but did not work. I will appreciate if you suggest a way to draw the figure properly. Also, is there a way to label each individual pie chart?

labes1,values1=[0, 1], [0.7099673202614379, 0.2900326797385621]
labels2,values2=['Pass', 'Withdrawn', 'Fail', 'Distinction'],[0.3510392609699769,0.33102386451116245,0.2763664357197845,0.04157043879907621]

fig = plt.figure(figsize=(5,5),dpi=300)
ax1 = fig.add_subplot(121)
ax1.pie(values1,radius=1.5,autopct='%0.2f%%',labels=labels1,label="")
ax2 = fig.add_subplot(122)
ax2.pie(values2,radius=1.5,autopct='%0.2f%%',labels=labels2,label="")
plt.subplots_adjust(left=0.1,
                    bottom=0.1,
                    right=0.9,
                    top=0.9,
                    wspace=0.4,
                    hspace=0.9)

plt.show()

enter image description here

Saif
  • 95
  • 8
  • If you draw a pie chart with only the data in the question, your intergraph adjustment values will be valid. `ax1.pie(values1, labels=labes1, autopct='%0.2f%%');ax2.pie(values2, labels=labels2,autopct='%0.2f%%')` – r-beginners Feb 16 '23 at 13:28
  • ax1.pie(values1, labels=labes1, autopct='%0.2f%%');ax2.pie(values2, labels=labels2,autopct='%0.2f%%'). I tried but it did not even show a pie ! – Saif Feb 16 '23 at 13:38
  • I tried tight_layout as i showed, it is commented in the code. the hspace was 0.4 and I changed to 0.9. – Saif Feb 16 '23 at 13:38
  • I editied the tried many versions to get it working and got confused when pasted it. – Saif Feb 16 '23 at 13:46
  • Try this: `ax1 = fig.add_subplot(121);ax1.pie(values1, labels=labes1, autopct='%0.2f%%');ax2 = fig.add_subplot(122);ax2.pie(values2, labels=labels2,autopct='%0.2f%%')` – r-beginners Feb 16 '23 at 13:50

1 Answers1

1

You can do this:

import matplotlib.pyplot as plt
import pandas as pd
labels1, values1 = [0, 1], [0.7099673202614379, 0.2900326797385621]
labels2, values2 = ['Pass', 'Withdrawn', 'Fail', 'Distinction'], [0.3510392609699769, 0.33102386451116245, 0.2763664357197845, 0.04157043879907621]

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14,5), dpi=300, gridspec_kw={'width_ratios': [1, 1.5]})

ax1.pie(values1, radius=1.5, autopct='%0.2f%%', labels=labels1)
ax1.set_title('Chart 1')

ax2.pie(values2, radius=1.5, autopct='%0.2f%%', labels=labels2)
ax2.set_title('Chart 2')

plt.show()

which gives

enter image description here