Place a Matplotlib legend outside the plot

Position a legend to the right or below a plot with bbox_to_anchor, and prevent clipping when displaying or saving the figure.

Best practices
Place a Matplotlib legend outside the plot

Use bbox_to_anchor to position the legend relative to the plotting axes:

plt.legend(loc='upper left', bbox_to_anchor=(1, 1))

For an axes legend, these are axes coordinates by default: (0, 0) is the lower-left corner and (1, 1) the upper-right. The legend's upper-left corner is anchored at the axes' upper-right, with the usual padding, placing the legend outside the plot.

Put the legend on the right

import matplotlib.pyplot as plt

x = [1, 2, 3]
y1 = [2, 4, 3]
y2 = [1, 3, 5]
plt.plot(x, y1, label='Series 1')
plt.plot(x, y2, label='Series 2')

plt.legend(loc='upper left', bbox_to_anchor=(1, 1))
plt.tight_layout()
plt.show()

tight_layout() adjusts the layout to make room for the participating artists, including the legend in this example.

Put the legend below the plot

After creating the labeled lines, use:

plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.08), ncol=3)
plt.tight_layout()

The anchor is centered horizontally and lies 8% of the axes' height below its lower boundary. ncol=3 arranges legend entries into three columns.

Prevent clipping in a saved image

tight_layout() affects the figure's layout and therefore its saved appearance too. It is not limited to the on-screen display. Figure dimensions or artists excluded from layout calculations can still cause clipping.

plt.savefig('chart.png', bbox_inches='tight', dpi=150)

bbox_inches='tight' computes a tight output boundary around the included artists. Custom artists may require bbox_extra_artists. Save before plt.show(), or keep a reference to the figure and call fig.savefig().

Position reference

Position loc bbox_to_anchor
Outside right upper left (1, 1)
Below upper center (0.5, -0.08)
Above lower center (0.5, 1.02)
Outside left upper right (-0.05, 1)

FAQ

How do I create one legend for several subplots?

Collect the handles and labels, create the figure with layout='constrained', and use fig.legend(handles, labels, loc='outside right center') in Matplotlib 3.7 or later. Constrained layout reserves space for that outside legend. With older versions, collect handles manually and position fig.legend() using an anchor, reserving figure space yourself.

What if the legend covers the data?

Move it outside, or try loc='best' to choose a location within the axes. Dense plots often benefit from an external legend.

What if a wide legend squeezes the plot?

Increase the figure width, move the legend below the plot, or adjust the layout manually. For example, plt.subplots_adjust(right=0.75) reserves the rightmost quarter of the figure after a tight_layout() call.

See the legend guide and savefig reference.

Related reading

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting