Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7169051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:54:23+00:00 2026-05-28T14:54:23+00:00

Reading the following article, I managed to put a legend outside plot. How to

  • 0

Reading the following article, I managed to put a legend outside plot.

  • How to put the legend out of the plot

code:

import matplotlib.pyplot as pyplot

x = [0, 1, 2, 3, 4]
y = [xx*xx for xx in x]

fig = pyplot.figure()
ax  = fig.add_subplot(111)

box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width*0.8, box.height])

ax.plot(x, y)
leg = ax.legend(['abc'], loc = 'center left', bbox_to_anchor = (1.0, 0.5))
#pyplot.show()

fig.savefig('aaa.png', bbox_inches='tight')

pyplot.show() displays the correct plot with a legend outside it. But when I save it as a file with fig.savefig(), the legend is truncated.

Some googling shows me workarounds such as adding bbox_extra_artists=[leg.legendPatch] or bbox_extra_artists=[leg] to savefig(), but neither worked.

What is the correct way to do it? Matplotlib version is 0.99.3.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:54:24+00:00Added an answer on May 28, 2026 at 2:54 pm

    The problem is that when you plot dynamically, matplotlib determines the borders automatically to fit all your objects.
    When you save a file, things are not being done automatically, so you need to specify
    the size of your figure, and then the bounding box of your axes object.
    Here is how to correct your code:

    import matplotlib.pyplot as pyplot
    
    x = [0, 1, 2, 3, 4]
    y = [xx*xx for xx in x]
    
    fig = pyplot.figure(figsize=(3,3))
    ax  = fig.add_subplot(111)
    
    #box = ax.get_position()
    #ax.set_position([0.3, 0.4, box.width*0.3, box.height])
    # you can set the position manually, with setting left,buttom, witdh, hight of the axis
    # object
    ax.set_position([0.1,0.1,0.5,0.8])
    ax.plot(x, y)
    leg = ax.legend(['abc'], loc = 'center left', bbox_to_anchor = (1.0, 0.5))
    
    fig.savefig('aaa.png')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading this DZone article about Java concurrency I was wondering if the following code:
I was reading the Wikipedia article on SFINAE and encountered following code sample: struct
Reading this article I've found a following piece of code: if ('v'=='\v') { //
I was reading the following article: http://msdn.microsoft.com/en-us/magazine/cc817398.aspx Solving 11 Likely Problems In Your Multithreaded
With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
I'm reading the Heroku Routing article and confused by the following: One Connection at
I am following the spring MVC tutorial and also reading this article on mvc
I am trying to do the following. Imagine you are reading some LINQ article
I am reading the following article, http://articles.sitepoint.com/article/hierarchical-data-database/2 aboutStoring Hierarchical Data in a Database. This
I was reading this article and I came across the following definition (in qglobal.h

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.