Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesTo move a Matplotlib legend outside a plot, use bbox_to_anchor with loc. Choose ax.legend() for a legend attached to one Axes, or fig.legend() for a shared legend across a Figure. The anchor coordinates are Axes-relative by default for an Axes legend and Figure-relative for a Figure legend.
Move one Axes legend outside the right edge
For a single plot, attach the legend to its Axes and place its upper-left corner just beyond the Axes’ upper-right edge:
As an Amazon Associate I earn from qualifying purchases.
fig, ax = plt.subplots()
ax.plot(x, y, label="Series")
ax.legend(loc="upper left", bbox_to_anchor=(1.02, 1))
Here, bbox_to_anchor=(1.02, 1) is an anchor point in Axes coordinates: x=1 is the right edge and y=1 is the top. The loc value chooses the legend corner that meets that point. Adjust the first coordinate to increase or reduce the gap. This right-side placement pattern is shown in the Matplotlib legend guide.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Understand how loc and bbox_to_anchor work together
bbox_to_anchor supplies a point or box; loc identifies which part of the legend is positioned against it. A two-value tuple, such as (1.02, 1), gives an anchor point. A four-value tuple, (x, y, width, height), defines a box in which the legend is placed. The Legend API documents both forms.
#1 Best Overall
For example, loc="upper left" attaches the legend’s upper-left corner to the supplied anchor. Changing loc changes the legend corner used, not the coordinate system. For many ordinary placements, loc alone is enough; use bbox_to_anchor when you need more precise positioning.
Choose the coordinate system explicitly when needed
The default anchor coordinate system depends on which object owns the legend: ax.legend() uses Axes coordinates, while fig.legend() uses Figure coordinates. If you want an Axes legend positioned relative to the whole Figure, set bbox_transform:
Rank #2
ax.legend(
loc="upper right",
bbox_to_anchor=(1, 1),
bbox_transform=fig.transFigure,
)
That places the legend’s upper-right corner at the Figure’s upper-right coordinate. The legend guide demonstrates this transform. The API also permits supplying a different transform when the default frame is not the one you want.
Use fig.legend() for a shared multi-panel legend
When several Axes contribute labeled artists to one shared legend, create a Figure legend rather than repeating a legend on every Axes. A Figure legend’s default anchor frame is the Figure:
fig.legend(
handles, labels,
loc="upper left",
bbox_to_anchor=(1.0, 1.0),
)
Provide the handles and labels you want represented, then place the legend relative to the Figure. For a Figure legend, a two-value anchor is a point; a four-value anchor defines a target box. See the Figure.legend API for its arguments.
Use constrained layout carefully for outside legends
Constrained layout can reserve space for an outside legend, but Matplotlib’s current documentation is inconsistent about Figure legends: the API documents outside-prefixed loc values for figure legends, while the constrained layout guide says Figure.legend is not yet handled. Check the result with your installed Matplotlib version, especially after saving.
Rank #4
For an Axes legend, the guide shows this layout-aware pattern:
Recommended Free Tools
fig, axs = plt.subplots(1, 2, layout="constrained")
# Plot labeled artists on the axes, then:
fig.legend(loc="outside right upper")
The order of the words matters: outside upper right reserves space above the plot, whereas outside right upper reserves space at the right. Enable constrained layout when creating the Figure. Calling tight_layout() turns constrained layout off.
Best Value
An outside Axes legend can cause constrained layout to shrink the subplot area to make room. If preserving the Axes size matters more, the guide demonstrates leg.set_in_layout(False) to exclude the legend from layout calculations. That can also leave the legend cropped, so it is not a universal fix.
Check the saved image for clipping
A legend placed beyond the Figure canvas may be clipped by the default export bounds. When appropriate, save with a tight bounding box:
fig.savefig("plot.png", bbox_inches="tight")
Inspect the exported file: layout and save settings interact, and a successful plotting call does not guarantee the legend is visible in the output. The tight layout guide explains how legends and annotations participate in layout calculations and how set_in_layout(False) excludes them. For precise extent measurements, the Legend API notes that a draw or save operation may be needed.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick 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.




