Adding Titles, Labels, and Legends
Once you've customized the style of your plot, the next step is to clearly label it. Labels tell viewers what the data means and make the chart easier to understand.
In this lesson, you'll learn how to add a title
, axis labels
, and a legend
to explain different data series.
Title
Use plt.title()
to add a heading to your chart.
Adding a Plot Title
plt.title("Monthly Revenue")
Axis Labels
Use plt.xlabel()
and plt.ylabel()
to label the x-axis and y-axis.
Adding Axis Labels
plt.xlabel("Month") plt.ylabel("Revenue ($)")
These labels help the reader understand what the axes represent.
Legends
If you plot more than one line, use label=
inside plt.plot()
and call plt.legend()
to display a legend.
Adding a Legend to Multiple Lines
plt.plot(x, y1, label="Product A") plt.plot(x, y2, label="Product B") plt.legend()
The legend will show which line corresponds to which label.
Quiz
0 / 1
How can you add a legend to a Matplotlib plot with multiple lines?
To add a legend to a plot with multiple lines in Matplotlib, use after specifying labels with plt.plot().
plt.title()
plt.legend()
plt.xlabel()
plt.ylabel()
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help