... matplotlib: legends


If you want to draw many lines then you could use a for loop combined with plt.plot. You can add a legend by adding a label to each plot and by calling plt.legend() afterwards.

import numpy as np
import matplotlib.pylab as plt

x = np.linspace(0, 1, 100)
for i in [1, 2, 3]:
    y = x ** i
    plt.plot(x, y, label=f"power={i}")

Again, note the semi-colon at the end here.

Feedback? See an issue? Something unclear? Feel free to mention it here.

If you want to be kept up to date, consider signing up for the newsletter.