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
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()
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()