Suppose you have multiple lines in the same plot, each of a different color, and
you wish to make a legend to tell what each line represents. You can do this
by passing on a label to each of the lines when you call plot()
, e.g., the
following line will be labelled "My Line 1".
ax.plot(x, y1, color="red", label="My Line 1")
This specifies the text that will appear in the legend for that line. Now to
make the actual legend visible, we can call ax.legend()
By default it will create a legend inside a box on the upper right hand corner
of the plot. You can pass arguments to legend()
to customize it. For instance
we can position it on the lower right hand corner, with out a frame box
surrounding it, and creating a title for the legend by calling the following:
ax.legend(loc="lower right", title="Legend Title", frameon=False)
Below is an example:
import matplotlib.pyplot as plt
# The data
x = [1, 2, 3]
y1 = [2, 15, 27]
y2 = [10, 40, 45]
y3 = [5, 25, 40]
# Initialize the figure and axes
fig, ax = plt.subplots(1, figsize=(8, 6))
# Set the title for the figure
fig.suptitle('Simple Legend Example ', fontsize=15)
# Draw all the lines in the same plot, assigning a label for each one to be
# shown in the legend
ax.plot(x, y1, color="red", label="My Line 1")
ax.plot(x, y2, color="green", label="My Line 2")
ax.plot(x, y3, color="blue", label="My Line 3")
# Add a legend with title, position it on the lower right (loc) with no box framing (frameon)
ax.legend(loc="lower right", title="Legend Title", frameon=False)
# Show the plot
plt.show()