Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Place a Matplotlib Legend Outside the Plot

Place a Matplotlib legend outside one Axes or share it across a Figure with the right combination of loc, bbox_to_anchor, layout settings, and export checks.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

For an Axes legend, the guide shows this layout-aware pattern:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.