Pass bbox_to_anchor alongside loc to ax.legend()—such as ax.legend(loc="upper left", bbox_to_anchor=(1.02, 1)) to place the legend cleanly to the right of the plot, or ax.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15), ncol=3) to center it horizontally below the x-axis—and save/render with bbox_inches="tight" (or layout="constrained") so the external legend is not clipped.
Basic Example
import matplotlib.pyplot as plt
fig, ax = plt.subplots(layout="constrained")
x = [1, 2, 3, 4]
ax.plot(x, [1, 4, 9, 16], label="Quadratic")
ax.plot(x, [1, 2, 3, 4], label="Linear")
ax.plot(x, [2, 3, 5, 7], label="Prime")
# 1. Place legend OUTSIDE to the upper-right of the Axes box
# loc="upper left" anchors the legend's top-left corner to (1.02, 1.0) in Axes coordinates
ax.legend(loc="upper left", bbox_to_anchor=(1.02, 1.0), borderaxespad=0)
# Save without clipping the external legend
fig.savefig("legend_outside_right.png", bbox_inches="tight")
plt.show()
Key Details & Positioning Mechanics
- How
locandbbox_to_anchorwork together: Think ofbbox_to_anchor=(x, y)as the pin location on the Axes (in normalized Axes coordinates where(0, 0)is bottom-left and(1, 1)is top-right), andlocas which corner/edge of the legend box attaches to that pin:- Outside Right (top-aligned):
loc="upper left", bbox_to_anchor=(1.02, 1.0)— pins the legend’s upper-left corner just past the right edge (x = 1.02) at the top (y = 1.0). - Outside Right (vertically centered):
loc="center left", bbox_to_anchor=(1.02, 0.5)— centers the legend vertically beside the right spine. - Outside Bottom (horizontal bar below x-axis):
loc="upper center", bbox_to_anchor=(0.5, -0.15), ncol=3— pins the top-center of a multi-column legend below the x-axis label. - Outside Top (horizontal banner above plot):
loc="lower center", bbox_to_anchor=(0.5, 1.02), ncol=3— places the legend cleanly above the plot area (leave room forax.set_title(..., pad=35)if using a title).
- Outside Right (top-aligned):
- Preventing clipped legends when saving or displaying (
bbox_inches="tight"/layout="constrained"):- When calling
fig.savefig("out.png"), Matplotlib only exports the default figure canvas and will cut off legends pushed outsidex > 1.0ory < 0. Always passbbox_inches="tight"tosavefig(). - For interactive windows (
plt.show()), initialize the figure withplt.subplots(layout="constrained")(or callplt.tight_layout()), which automatically shrinks the Axes box to make room for the external legend.
- When calling
- Single shared legend for multiple subplots (
fig.legend()): When creating a multi-panel grid (fig, axes = plt.subplots(1, 2)), callfig.legend(...)instead ofax.legend(...)withloc="outside upper center"orloc="outside center right"(available in Matplotlib 3.7+ withlayout="constrained"):fig, axes = plt.subplots(1, 2, figsize=(8, 3.5), layout="constrained") axes[0].plot([1, 2], [3, 4], label="Series A") axes[1].plot([1, 2], [4, 2], label="Series B") # Matplotlib 3.7+ built-in 'outside' placement on Figure legends fig.legend(loc="outside center right")
Common Outside Legend Placement Reference
| Desired Position | loc Argument |
bbox_to_anchor Coordinates |
Recommended Extra Arguments |
|---|---|---|---|
| Right (top-aligned) | "upper left" |
(1.02, 1.0) |
borderaxespad=0 |
| Right (vertically centered) | "center left" |
(1.02, 0.5) |
borderaxespad=0 |
| Bottom (below x-axis label) | "upper center" |
(0.5, -0.15) |
ncol=3 (adjust -0.15 to -0.22 if x-label overlaps) |
| Top (above plot area) | "lower center" |
(0.5, 1.02) |
ncol=3, borderaxespad=0 |
| Left (outside y-axis) | "upper right" |
(-0.15, 1.0) |
borderaxespad=0 (increase negative x to clear y-ticks) |
| Multi-subplot figure edge | "outside upper center" / "outside center right" |
Not needed on fig.legend() (Matplotlib 3.7+) |
Initialize with plt.subplots(..., layout="constrained") |
You must log in or register to comment.
