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.
plt.title("Monthly Revenue")
Axis Labels
Use plt.xlabel()
and plt.ylabel()
to label the x-axis and y-axis.
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.
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.
What’s Next?
Now that your plots are fully labeled and explained, we’ll explore bar and pie charts to visualize data that isn’t best shown as a line.
How can you add a legend to a Matplotlib plot with multiple lines?
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help