HOWTO · Matplotlib
How to Specify the Legend Position in Graph Coordinates in Matplotlib
This article shows you how to specify the legend's position in a graph in Matplotlib.
On this page
To position a Matplotlib legend, set its loc parameter to a standard location such as "upper left" or "lower right". To place it at a particular coordinate, combine loc with bbox_to_anchor: bbox_to_anchor supplies the anchor coordinate, and loc selects the legend point aligned to it. Matplotlib’s default borderaxespad adds a small gap, so the visible legend point may sit slightly away from the coordinate; set borderaxespad=0 when exact alignment is needed.
Examples: Position a Matplotlib Legend with loc and bbox_to_anchor
The following example creates two axes and gives each one a legend. It uses best on the first axes and lower left on the second. You can change these values to compare standard placements for your own plotted data. The figures show the resulting plots; the code shows which settings to adapt.
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [0.5 * i + 1 for i in x]
y2 = [2 * i + 1 for i in x]
fig, ax = plt.subplots(2, 1)
ax[0].plot(x, y1, "b-", label="0.5x+1")
ax[0].plot(x, y2, "g-", label="2x+1")
ax[0].legend(loc="best")
ax[1].plot(x, y1, "b-", label="0.5x+1")
ax[1].plot(x, y2, "g-", label="2x+1")
ax[1].legend(loc="lower left")
plt.tight_layout()
plt.show()
The loc parameter accepts values such as best, upper right, upper left, lower left, lower right, right, center left, center right, lower center, upper center, and center:
best |
upper right |
upper left |
lower left |
lower right |
right |
center left |
center right |
lower center |
upper center |
center |
For a standard location, loc positions the legend within its axes. The best value lets Matplotlib choose a placement based on the plotted contents; named values such as lower left let you choose the location directly. If a legend covers an important line or marker, try another value from the list. These choices describe the legend’s placement relative to its axes, rather than a position shared by the whole figure.
For a coordinate-based placement, bbox_to_anchor gives the anchor coordinate and loc selects which point of the legend is aligned to it. In the next example, each anchor is specified in coordinates relative to its own axes. The red marker is drawn at that same coordinate using ax.transAxes, so it makes the anchor easy to see. The two axes use different loc values to demonstrate how the selected legend point changes. With the default borderaxespad, the legend sits slightly away from each marker; the marker still identifies the anchor coordinate.
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [0.5 * i + 1 for i in x]
y2 = [2 * i + 1 for i in x]
fig, ax = plt.subplots(2, 1)
ax[0].plot(x, y1, "b-", label="Plot-1")
ax[0].plot(x, y2, "g-", label="Plot-2")
ax[0].legend(loc="upper left", bbox_to_anchor=(0.9, 0.75))
ax[0].scatter((0.9), (0.75), s=70, c="red", transform=ax[0].transAxes)
ax[1].plot(x, y1, "b-", label="Plot-1")
ax[1].plot(x, y2, "g-", label="Plot-2")
ax[1].legend(loc="center right", bbox_to_anchor=(0.6, 0.4))
ax[1].scatter((0.6), (0.4), s=70, c="red", transform=ax[1].transAxes)
plt.tight_layout()
plt.show()
In the upper axes, bbox_to_anchor=(0.9, 0.75) places the anchor at the red marker. Because loc="upper left", the legend’s upper-left point is aligned to that anchor, with a small visible gap from the default borderaxespad. In the lower axes, the anchor is (0.6, 0.4) and loc="center right", so the legend’s center-right point is aligned to the lower red marker with the same padding effect. To make either selected point coincide exactly with its marker, add borderaxespad=0 to that axes’ legend call.
To adapt either placement, first choose an x and y coordinate relative to the relevant axes, then set the matching bbox_to_anchor tuple and mark that point while adjusting the plot. Select a loc value for the legend point you want aligned there. The two settings work together: changing only the anchor moves that selected point, while changing loc changes which point of the legend is aligned to the anchor. Keep each marker’s transform tied to the same axes as its legend so the visual reference uses the same coordinate space. Leave the default borderaxespad for a small gap, or set borderaxespad=0 in the legend call for exact marker alignment. No console output is shown; the figures above show the resulting plots.