Lecture

Adding Titles, Labels, and Legends

After styling your plot, the next step is to communicate meaning clearly through labels.

Titles and axis labels help readers understand what they’re looking at, while legends make it easy to distinguish between multiple data series.

In this lesson, you’ll learn how to add a chart title, axis labels, and a legend using simple Matplotlib functions.


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 automatically matches each line to its label, making multi-line charts easier to interpret at a glance.

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