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 9017441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:16:35+00:00 2026-06-16T04:16:35+00:00

I have a figure with two subplots as 2 rows and 1 column. I

  • 0

I have a figure with two subplots as 2 rows and 1 column. I can add a nice looking figure legend with

fig.legend((l1, l2), ['2011', '2012'], loc="lower center", 
           ncol=2, fancybox=True, shadow=True, prop={'size':'small'})

However, this legend is positioned at the center of the figure and not below the center of the axes as I would like to have it. Now, I can obtain my axes coordinates with

axbox = ax[1].get_position()

and in theory I should be able to position the legend by specifying the loc keyword with a tuple:

fig.legend(..., loc=(axbox.x0+0.5*axbox.width, axbox.y0-0.08), ...)

This works, except that the legend is left aligned so that loc specifies the left edge/corner of the legend box and not the center. I searched for keywords such as align, horizontalalignment, etc., but couldn’t find any. I also tried to obtain the “legend position”, but legend doesn’t have a *get_position()* method. I read about *bbox_to_anchor* but cannot make sense of it when applied to a figure legend. This seems to be made for axes legends.

Or: should I use a shifted axes legend instead? But then, why are there figure legends in the first place? And somehow it must be possible to “center align” a figure legend, because loc=”lower center” does it too.

Thanks for any help,

Martin

  • 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-06-16T04:16:36+00:00Added an answer on June 16, 2026 at 4:16 am

    In this case, you can either use axes for figure legend methods. In either case, bbox_to_anchor is the key. As you’ve already noticed bbox_to_anchor specifies a tuple of coordinates (or a box) to place the legend at. When you’re using bbox_to_anchor think of the location kwarg as controlling the horizontal and vertical alignment.

    The difference is just whether the tuple of coordinates is interpreted as axes or figure coordinates.

    As an example of using a figure legend:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
    
    x = np.linspace(0, np.pi, 100)
    
    line1, = ax1.plot(x, np.cos(3*x), color='red')
    line2, = ax2.plot(x, np.sin(4*x), color='green')
    
    # The key to the position is bbox_to_anchor: Place it at x=0.5, y=0.5
    # in figure coordinates.
    # "center" is basically saying center horizontal alignment and 
    # center vertical alignment in this case
    fig.legend([line1, line2], ['yep', 'nope'], bbox_to_anchor=[0.5, 0.5], 
               loc='center', ncol=2)
    
    plt.show()
    

    enter image description here

    As an example of using the axes method, try something like this:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
    
    x = np.linspace(0, np.pi, 100)
    
    line1, = ax1.plot(x, np.cos(3*x), color='red')
    line2, = ax2.plot(x, np.sin(4*x), color='green')
    
    # The key to the position is bbox_to_anchor: Place it at x=0.5, y=0
    # in axes coordinates.
    # "upper center" is basically saying center horizontal alignment and 
    # top vertical alignment in this case
    ax1.legend([line1, line2], ['yep', 'nope'], bbox_to_anchor=[0.5, 0], 
               loc='upper center', ncol=2, borderaxespad=0.25)
    
    plt.show()
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two versions of a Windows Forms Application and can't figure for the
I have two questions. How can I get the spinner in the figure below
In two days i have an exam in java, and i can not figure
How can I add scrollbar to the subplots? I have gone through many tutorials
I have two subplots in a figure. I want to set the axes of
I have two divs which should looks like one figure. The problem is with
In above figure the element Chandru is repeated as two times. so i have
Say that I have two figures in matplotlib, with one plot per figure: import
I'm trying to figure out how it is that two variable types that have
Hope find a guru's help to figure out the next problem. I have two

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.