0

I am using subplot to display some figures, however the labels are mixed with the last subplot, so the plots don't have equal size. and the previous 5 are not perfectly round circle.

Here's my code:

for i in range(6):
    plt.subplot(231 + i)
    plt.title("Department " + depts[i])
    labels = ['Male', 'Female']
    colors = ['#3498DB', '#E74C3C']
    sizes = [male_accept_rates[i] / (male_accept_rates[i] + female_accept_rates[i]),
             female_accept_rates[i] / (male_accept_rates[i] + female_accept_rates[i])]
    patches, texts = plt.pie(sizes, colors=colors, startangle=90)
plt.axis('equal')
plt.tight_layout()
plt.legend(labels, loc="best")
plt.show()

And here's the output: piecharts

can anyone give me some advise? Much appreciated.

Adam Liu
  • 1,288
  • 13
  • 17

1 Answers1

1

It appears plt.axis('equal') only applies to the last subplot. So your fix is to put that line of code in the loop.

So:

import numpy as np
import matplotlib.pyplot as plt

depts = 'abcdefg'
male_accept_rates =  np.array([ 2, 3, 2, 3, 4, 5], float)
female_accept_rates= np.array([ 3, 3, 4, 3, 2, 4], float)

for i in range(6):
    plt.subplot(231 + i)
    plt.title("Department " + depts[i])
    labels = ['Male', 'Female']
    colors = ['#3498DB', '#E74C3C']
    sizes = [male_accept_rates[i] / (male_accept_rates[i] + female_accept_rates[i]),
             female_accept_rates[i] / (male_accept_rates[i] + female_accept_rates[i])]
    patches, texts = plt.pie(sizes, colors=colors, startangle=90)
    plt.axis('equal')                                                                                          
plt.tight_layout()                                                                                             
plt.legend(labels, loc="best")                                                                                 
plt.show()

Produces this now: enter image description here

Douglas Dawson
  • 556
  • 2
  • 9
  • Just wanna ask a follow up question, the labels are laying on top of the last pie, which is not what I wanted. Is there any way to put the labels somewhere else? Thanks. – Adam Liu Mar 06 '17 at 13:11
  • Yeah, by default, the legend will be drawn for the latest subplot. For getting a single legend for a set of subplots, I'd look here: http://stackoverflow.com/questions/9834452/how-do-i-make-a-single-legend-for-many-subplots-with-matplotlib I tried those solutions and they did not work perfectly for me, but my version of matplotlib is a little different. They may work for you. – Douglas Dawson Mar 06 '17 at 15:30