Adding Labels and Titles
You'll learn how to annotate your Matplotlib plots with axis labels and titles to make them clear and interpretable.
Why Annotate Your Plots?
A plot without labels is like a map without street names—technically it shows the data, but nobody knows what they're looking at. After you've customized your lines with colors and styles, the next critical step is telling your audience what those lines actually represent.
Three Essential Annotation Functions
Matplotlib provides three straightforward functions to add text annotations:
xlabel()– Labels the horizontal (x) axisylabel()– Labels the vertical (y) axistitle()– Adds a title above the plot
These work with both the pyplot and object-oriented interfaces you learned earlier.
Basic Usage
import matplotlib.pyplot as plt
# Sample data
months = [1, 2, 3, 4, 5]
sales = [150, 200, 180, 220, 250]
# Create plot
plt.plot(months, sales, color='blue', marker='o')
# Add annotations
plt.xlabel('Month')
plt.ylabel('Sales ($1000s)')
plt.title('Monthly Sales Performance')
plt.show()
Each function takes a string argument. The text you provide should clearly describe what the axis represents, including units when relevant. Your title should summarize what the entire visualization shows.
Think of It as Storytelling
Just as you learned about perceptual accuracy and visual encoding, labels complete the story. They transform raw visual marks into meaningful information. Without "Month" and "Sales ($1000s)", your audience would be guessing what the blue line with circles represents.
Key Takeaway: Always use xlabel(), ylabel(), and title() to annotate your plots—these three functions transform ambiguous visuals into clear, interpretable data stories.