Skip to content
LearnPY
Matplotlibfunction reference

Axes.legend

Show a legend for labeled artists.

Signature

Axes.legend(loc='best', frameon=True)

What it does

Axes.legend reads the label arguments from plotted artists and builds a key. Call it after the series you want included. loc controls placement, while frameon controls the background box.

Parameters

locstr, optionaloptional
Placement such as 'upper right' or 'best'.
frameonbool, optionaloptional
Whether to draw a frame behind the legend.

Returns

Legend

Example

pythonAxes.legend example
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [2, 4, 3], label="actual")
ax.plot([1, 2, 3], [3, 3, 3], label="target")
ax.legend(loc="upper left", frameon=False)

Watch out for

  • Series without a label do not appear in the legend.
  • Calling legend before plotting can produce an empty legend.