Skip to main content
Practice

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.


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.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.