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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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: stackoverflow.com/questions/9834452/… 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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.