iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use ax.legend() to position a legend for one Axes, or fig.legend() for a shared legend across a Figure. Add bbox_to_anchor to place it freely: loc determines which point on the legend attaches to the anchor, and the coordinate system depends on whether the legend belongs to an Axes or the Figure.
Move one Axes legend outside the plot
For a single plot, anchor the legend just beyond the Axes’ right edge:
fig, ax = plt.subplots()
ax.plot(x, y, label="Series")
ax.legend(loc="upper left", bbox_to_anchor=(1.02, 1))
With ax.legend(), the anchor uses Axes coordinates by default: (0, 0) is the Axes’ lower-left corner and (1, 1) its upper-right corner. Here, loc="upper left" attaches the legend’s upper-left corner to the anchor at (1.02, 1), just to the right of the Axes. This is the right-side placement pattern in the Matplotlib legend guide.
Recommended Free Tools
Anchor an Axes legend to the Figure instead
If the position should be relative to the entire Figure, explicitly set the transform:
#1 Best Overall
ax.legend(
loc="upper right",
bbox_to_anchor=(1, 1),
bbox_transform=fig.transFigure,
)
Now (1, 1) refers to the Figure’s upper-right corner rather than the Axes. Matplotlib’s guide demonstrates this transform for placing an Axes legend in Figure coordinates.
Use bbox_to_anchor and loc together
bbox_to_anchor accepts a bounding box, a two-value tuple, or a four-value tuple. A two-value tuple, such as (x, y), specifies an anchor point; loc determines which legend corner or edge meets that point. A four-value tuple, (x, y, width, height), defines a box in which the legend is placed. The anchor’s coordinate system follows the legend’s parent by default: Axes coordinates for ax.legend() and Figure coordinates for fig.legend(). Set bbox_transform to override it. See the Legend API for the accepted forms and parameters.
Rank #2
For basic placement, loc alone may be enough. Use bbox_to_anchor when the default legend positions do not give you the placement you need.
Add one legend for multiple subplots
Use fig.legend() when one legend should describe artists across several Axes. For example:
fig, axs = plt.subplots(1, 2, layout="constrained")
# Plot labeled artists on the axes, then:
fig.legend(loc="outside right upper")
Matplotlib’s current legend API documents outside-prefixed location strings for Figure legends. The order matters: outside upper right reserves space above the Axes, while outside right upper reserves space at the right. The Figure.legend API describes these locations.
There is a documented limitation to keep in mind: the constrained-layout guide says constrained layout handles outside Axes legends but does not yet handle Figure.legend(). Because the API and guide describe different aspects of this behavior, verify the result with the Matplotlib version you use and inspect the saved image.
Position a Figure legend manually
For direct control over a Figure-wide legend, specify its anchor explicitly:
fig.legend(
handles, labels,
loc="upper left",
bbox_to_anchor=(1.0, 1.0),
)
For fig.legend(), the default anchor coordinates are Figure coordinates. A two-value tuple places the corner named by loc at the anchor; use a four-value tuple to define a placement box. Supply bbox_transform if you need a different coordinate frame.
Best Value
Make room for the legend and check the saved image
Layout and export are separate concerns. A legend can appear correctly in an interactive window but be clipped when the Figure is saved, particularly if it sits beyond the Figure’s default canvas bounds.
- Enable constrained layout when creating the Figure, for example with
plt.subplots(layout="constrained"). Callingtight_layout()turns constrained layout off. - An outside Axes legend can cause constrained layout to shrink the subplot area to make room. If you need to keep the Axes size fixed,
leg.set_in_layout(False)can exclude the legend from layout calculations, but the legend may then be cropped. - When content extends past the canvas, save with
fig.savefig("plot.png", bbox_inches="tight")to include artists beyond the default bounds. Inspect the exported file to confirm that the legend is visible and the plot layout is acceptable. - Matplotlib’s tight-layout guide also documents that legends and annotations participate in layout calculations and can be excluded with
set_in_layout(False). This is a targeted layout adjustment, not a universal clipping fix.
Legend extent measurements can depend on drawing and the output backend. The Legend API notes that accurate extents may require saving the Figure or using draw_without_rendering.
Choose the placement method by scope and layout
| Approach | Best for | Default anchor coordinates | Layout consideration |
|---|---|---|---|
ax.legend() with bbox_to_anchor |
A legend for one Axes, placed outside it | Axes | Constrained layout can reserve room for outside Axes legends. |
ax.legend() with bbox_transform=fig.transFigure |
A legend for one Axes, positioned relative to the whole Figure | Figure, when the transform is supplied | Check the saved output if the legend sits beyond the canvas. |
fig.legend() with an outside loc |
A shared legend for multiple Axes | Figure | The constrained-layout guide documents that Figure legends are not yet handled. |
fig.legend() with bbox_to_anchor |
A shared legend with manually specified placement | Figure | Confirm the final layout and export; constrained layout may not reserve space for it. |
For outside locations, layout behavior, and export options, consult Matplotlib’s constrained-layout guide and tight-layout guide.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallQuick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

