You are reading an old version of the documentation (v1.3.0). For the latest version see https://matplotlib.org/stable/
matplotlib

This Page

pylab_examples example code: legend_demo2.pyΒΆ

(Source code, png, hires.png, pdf)

../../_images/legend_demo21.png
# Make a legend for specific lines.
import matplotlib.pyplot as plt
import numpy as np


t1 = np.arange(0.0, 2.0, 0.1)
t2 = np.arange(0.0, 2.0, 0.01)

# note that plot returns a list of lines.  The "l1, = plot" usage
# extracts the first element of the list into l1 using tuple
# unpacking.  So l1 is a Line2D instance, not a sequence of lines
l1, = plt.plot(t2, np.exp(-t2))
l2, l3 = plt.plot(t2, np.sin(2 * np.pi * t2), '--go', t1, np.log(1 + t1), '.')
l4, = plt.plot(t2, np.exp(-t2) * np.sin(2 * np.pi * t2), 'rs-.')

plt.legend( (l2, l4), ('oscillatory', 'damped'), 'upper right', shadow=True)
plt.xlabel('time')
plt.ylabel('volts')
plt.title('Damped oscillation')
plt.show()

Keywords: python, matplotlib, pylab, example, codex (see Search examples)