108

I am aware of the bbox_to_anchor keyword and this thread, which very helpfully suggests how to manually place the legend:

How to put the legend out of the plot

However, I'd like to use the coordinates of my x- and y-axis in the graph to specify the legend position (inside the plot), as I might need to move the figure into a large figure with a different axis environment, and I don't want to manually play around with those coordinates every time I do this. Is this possible?

Edit: A small example is here:

import numpy as n
f, axarr = plt.subplots(2,sharex=True)
axarr[1].set_ylim([0.611,0.675])
axarr[0].set_ylim([0.792,0.856]) 
axarr[0].plot([0, 0.04, 0.08],n.array([ 0.83333333,  0.82250521,0.81109048]), label='test1') 
axarr[0].errorbar([0, 0.04, 0.08],n.array([ 0.8,  0.83,   0.82]),n.array([0.1,0.1,0.01]), label='test2') 
axarr[1].plot([0, 0.04, 0.08],n.array([ 0.66666667,  0.64888304,  0.63042428]))
axarr[1].errorbar([0, 0.04, 0.08],n.array([ 0.67,  0.64,  0.62]),n.array([ 0.01,  0.05,  0.1]))
axarr[0].legend(bbox_to_anchor=(0.04, 0.82, 1., .102),labelspacing=0.1,       handlelength=0.1, handletextpad=0.1,frameon=False, ncol=4, columnspacing=0.7)

enter image description here

I think what confuses me is that the legend does not actually start at 0.82, and indeed for my larger plot (with 5 subplots of this type), I need to use legend coordinates bbox_to_anchor=(0.04, 1.15, 1., .102) in order to make the legend appear on coordinates (0.02, 0.83). But maybe I am getting something else wrong?

7
  • Its not exactly clear in how far the solution from the linked question do not help you as by default the bbox_to_anchor argument takes the axes coordinates, just as you want it to be. You may want to give an example of what you are trying to achieve and/or better explain in how far the solutions are not what you are looking for. Commented Jun 7, 2017 at 12:47
  • Thanks - I've edited it. But maybe you're right and I'm just misunderstanding something with how matplotlib places these legends in general - do you know which corner of the legend is placed on the coordinates that are given to bbox_to_anchor?
    – mzzx
    Commented Jun 7, 2017 at 13:25
  • Well I thought I had a complete explanation into my answer to the linked question. There is also the link to this question included that shows how to interprete the 4-tuple bbox_to_anchor specification. I may still try to answer your question here, but for that I would need to know what exactly you mean when asking for "the legend appear on coordinates (0.02, 0.83)" is it the lower left corner that you want to have there? Commented Jun 7, 2017 at 13:33
  • Ouff sorry I thought I'd looked through the other post carefully, but I hadn't scrolled down far enough to see your answer - thank you, trying to understand this now. And yes, I did want to lower left corner to be at these coordinates.
    – mzzx
    Commented Jun 7, 2017 at 13:40
  • Ok sorry, just to check if I get this right: so when I just say loc="upper right", and give no bbox_to_anchor specification, matplotlib interprets that as the loc with respect to the axes. But when I say loc="upper right" and give a bbox_to_anchor specification, that bbox_to_anchor specification will be interpreted with respect to the axes and the loc keyword refers to the corner of the legend?
    – mzzx
    Commented Jun 7, 2017 at 13:45

5 Answers 5

161

The loc parameter specifies in which corner of the bounding box the legend is placed. The default for loc is loc="best" which gives unpredictable results when the bbox_to_anchor argument is used.
Therefore, when specifying bbox_to_anchor, always specify loc as well.

The default for bbox_to_anchor is (0,0,1,1), which is a bounding box over the complete axes. If a different bounding box is specified, is is usually sufficient to use the first two values, which give (x0, y0) of the bounding box.

Below is an example where the bounding box is set to position (0.6,0.5) (green dot) and different loc parameters are tested. Because the legend extents outside the bounding box, the loc parameter may be interpreted as "which corner of the legend shall be placed at position given by the 2-tuple bbox_to_anchor argument".

enter image description here

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = 6, 3
fig, axes = plt.subplots(ncols=3)
locs = ["upper left", "lower left", "center right"]
for l, ax in zip(locs, axes.flatten()):
    ax.set_title(l)
    ax.plot([1,2,3],[2,3,1], "b-", label="blue")
    ax.plot([1,2,3],[1,2,1], "r-", label="red")
    ax.legend(loc=l, bbox_to_anchor=(0.6,0.5))
    ax.scatter((0.6),(0.5), s=81, c="limegreen", transform=ax.transAxes)

plt.tight_layout()    
plt.show()

See especially this answer for a detailed explanation and the question What does a 4-element tuple argument for 'bbox_to_anchor' mean in matplotlib? .


If you want to specify the legend position in other coordinates than axes coordinates, you can do so by using the bbox_transform argument. If may make sense to use figure coordinates

ax.legend(bbox_to_anchor=(1,0), loc="lower right",  bbox_transform=fig.transFigure)

It may not make too much sense to use data coordinates, but since you asked for it this would be done via bbox_transform=ax.transData.

0
38

Way I use very often is loc argument in the legend function. String input works well:

plt.legend(loc = "upper left")

As documentation says, for the string referencing you can use:

        ===============   =============
        Location String   Location Code
        ===============   =============
        'best'            0
        'upper right'     1
        'upper left'      2
        'lower left'      3
        'lower right'     4
        'right'           5
        'center left'     6
        'center right'    7
        'lower center'    8
        'upper center'    9
        'center'          10
        ===============   =============
1
  • 1
    This does not answer the users question, these are not graph coordinates
    – Jlanday
    Commented Feb 23, 2023 at 17:11
24

According to the matplotlib legend documentation:

The location can also be a 2-tuple giving the coordinates of the lower-left corner of the legend in axes coordinates (in which case bbox_to_anchor will be ignored).

Thus, one could use:

plt.legend(loc=(x, y))

to set the legend's lower left corner to the specified (x, y) position.

Note that the x and y coordinates here are relative, meaning that x=0 is the left most point in the plot and x=1 is the rightmost point in the plot. Similarly for y=0 and y=1 along the height of the plot, where y=0 is the bottom, and y=1 is the top.

10

You can change location of legend using loc argument. https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend

import matplotlib.pyplot as plt

plt.subplot(211)
plt.plot([1,2,3], label="test1")
plt.plot([3,2,1], label="test2")
# Place a legend above this subplot, expanding itself to
# fully use the given bounding box.
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
           ncol=2, mode="expand", borderaxespad=0.)

plt.subplot(223)
plt.plot([1,2,3], label="test1")
plt.plot([3,2,1], label="test2")
# Place a legend to the right of this smaller subplot.
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

plt.show()
3
  • 2
    Sorry - how to the bbox_to_anchor and loc keywords interact? Will bbox_to_anchor place the legend at a position within the loc environment?
    – mzzx
    Commented Jun 7, 2017 at 13:30
  • loc argument will inform matplotlib which part of the bounding box of the legend should be placed at the arguments of bbox_to_anchor. Commented Jun 7, 2017 at 13:34
  • 1
    @mzzx if you find this answer helpful (independent of whether you accept it or not) you may still upvote - which is kind of the same as saying "thanks". Commented Jun 7, 2017 at 14:08
3

In addition to @ImportanceOfBeingErnest's post, I use the following line to add a legend at an absolute position in a plot.

plt.legend(bbox_to_anchor=(1.0,1.0),\
    bbox_transform=plt.gcf().transFigure)

For unknown reasons, bbox_transform=fig.transFigure does not work with me.

1
  • This is probably because you have more than one figure. To select the current figure you could use plt.figure(fig.number) and then use your fig.transFigure command, it should work.
    – toti08
    Commented Apr 22, 2021 at 8:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.