You are reading an old version of the documentation (v3.1.3). For the latest version see https://matplotlib.org/stable/gallery/text_labels_and_annotations/legend.html
Version 3.1.3
matplotlib
Fork me on GitHub

Table of Contents

Related Topics

Legend using pre-defined labels

Defining legend labels with plots.

import numpy as np
import matplotlib.pyplot as plt

# Make some fake data.
a = b = np.arange(0, 3, .02)
c = np.exp(a)
d = c[::-1]

# Create plots with pre-defined labels.
fig, ax = plt.subplots()
ax.plot(a, c, 'k--', label='Model length')
ax.plot(a, d, 'k:', label='Data length')
ax.plot(a, c + d, 'k', label='Total message length')

legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large')

# Put a nicer background color on the legend.
legend.get_frame().set_facecolor('C0')

plt.show()
../../_images/sphx_glr_legend_001.png

References

The use of the following functions, methods, classes and modules is shown in this example:

import matplotlib
matplotlib.axes.Axes.plot
matplotlib.pyplot.plot
matplotlib.axes.Axes.legend
matplotlib.pyplot.legend

Out:

<function legend at 0x7f18a57955e0>

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery