Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position and align a matplotlib figure legend?

Tags:

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

like image 951
maschu Avatar asked Dec 15 '12 17:12

maschu


People also ask

How do I fix the legend position in Matplotlib?

To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.

How do I change the order of items in Matplotlib legend?

Change order of items in the legend The above order of elements in the legend region can be changed by the gca method that uses another sub-method called get_legend_handles_labels method. These handles and labels lists are passed as parameters to legend method with order of indexes.

How do I make the legend horizontal in Matplotlib?

Set the figure size and adjust the padding between and around the subplots. Using plot() method, plot lines with the labels line1, line2 and line3. Place a legend on the figure using legend() method, with number of labels for ncol value in the argument.


1 Answers

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

like image 143
Joe Kington Avatar answered Sep 28 '22 07:09

Joe Kington