0

I am trying to plot two donut charts side by side using matplotlib in python.

First of all I am creating the donut chart using the following logic:

  1. Create a simple pie chart;
  2. Pick a position for the chart using the add_axes function;
  3. Add a patch (white circle in it so it can look like a donut chart);
  4. Display the side by side charts on the screen.

    fig= plt.figure()
    circle1 = plt.Circle((0,0), radius= .7 ,color='white')
    
    ax1= fig.add_axes([0,0,1,1],aspect=1)
    ax1.pie(x= values, labels= KA, startangle=30,radius=1.2)
    ax1.add_patch(circle1)
    
    circle2 = plt.Circle((0,0), radius= .7 ,color='white')
    ax2 = fig.add_axes([1,0,1,1],aspect=1)
    ax2.pie(x=values2, explode=explode, labels=KA2,startangle=30,radius=1.2)
    ax2.add_patch(circle2)
    
    plt.show()
    

When it runs the function plt.show() it only displays the first donut chart, with a very strong zoom in it...

What has been driving me crazy is the fact that if I run the following function: plt.savefig('testplot.png',bbox_inches='tight') it saves a png file just like I want...

How can I do that on the plt.show()

1 Answer 1

2

You create the second axes outside the figure. The figure goes from 0 to 1 in both directions. If you start your second axes at position 1, it will go from 1 to 2, which is outside the shown figure.

Either use subplots

fig, (ax1, ax2) = plt.subplots(subplot_kw={"aspect" : 1})

Or create the axes within the figure boundary,

ax1 = fig.add_axes([0,0,0.45,1], aspect=1)
ax2 = fig.add_axes([0.55,0,0.45,1], aspect=1)
Sign up to request clarification or add additional context in comments.

Comments

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.