Labels are essential in any data visualization—they help the viewer understand what the data represents. In Matplotlib, labels include:
-
Axis labels (
xlabel
,ylabel
) -
Titles (
title
) -
Legends (
legend
) -
Tick labels (
xticks
,yticks
) -
Annotations (
annotate
)
This guide shows how to use each type, customize them, and avoid common issues.
Setup: Basic Plot Structure
Start by importing Matplotlib and creating a sample plot:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]
plt.plot(x, y)
1. Adding Axis Labels
Use plt.xlabel()
and plt.ylabel()
to name the x and y axes.
plt.xlabel("Time (days)")
plt.ylabel("Sales ($)")
Customizing Axis Labels
You can set font size, color, and font weight:
plt.xlabel("Time (days)", fontsize=14, color='blue', fontweight='bold')
plt.ylabel("Sales ($)", fontsize=14, color='green')
2. Adding a Plot Title
Use plt.title()
to give your plot a descriptive heading.
plt.title("Daily Sales Over Time")
Title Customization
plt.title("Daily Sales Over Time", fontsize=16, color='purple', loc='center')
-
loc='left'
,'center'
, or'right'
aligns the title. -
You can also use
pad
to adjust the spacing above the plot.
plt.title("Sales Data", pad=20)
3. Adding a Legend
If you're plotting multiple lines, use label
in plot()
and then call plt.legend()
.
plt.plot(x, y, label="Product A", color='blue')
plt.legend()
Legend Customization
plt.legend(loc='upper left', fontsize=12, title='Legend')
Common legend locations:
-
'upper left'
,'upper right'
,'lower left'
,'center'
, etc.
To place the legend outside the plot:
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
4. Customizing Tick Labels
Change Tick Values
plt.xticks([1, 2, 3, 4, 5], ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'])
Rotate Tick Labels
plt.xticks(rotation=45)
plt.yticks(rotation=90)
Change Tick Label Font
plt.xticks(fontsize=12, fontweight='bold')
5. Adding Annotations
Use plt.annotate()
to add custom labels at specific data points.
plt.annotate("Peak", xy=(5, 30), xytext=(4, 32),
arrowprops=dict(facecolor='black', shrink=0.05))
-
xy
is the data point -
xytext
is where the label is displayed -
arrowprops
adds an arrow from the label to the point
✅ Full Example: Labeled Line Plot
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]
plt.figure(figsize=(8, 5))
# Line with label
plt.plot(x, y, label="Sales", color='blue', marker='o')
# Axis labels and title
plt.xlabel("Day", fontsize=12)
plt.ylabel("Sales ($)", fontsize=12)
plt.title("Sales Over 5 Days", fontsize=14, fontweight='bold')
# Tick customization
plt.xticks([1, 2, 3, 4, 5], ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], rotation=45)
# Annotation
plt.annotate("High Point", xy=(5, 30), xytext=(3.5, 32),
arrowprops=dict(facecolor='red', shrink=0.05))
# Legend
plt.legend(loc='upper left')
plt.grid(True)
plt.tight_layout()
plt.show()
Tips for Better Labels
Tip | Benefit |
---|---|
Use clear, concise axis titles | Improves plot readability |
Label units (e.g., "Sales ($)") | Prevents confusion |
Rotate tick labels for better fit | Avoids overlap |
Keep annotations short and relevant | Maintains clarity |
Use font size for hierarchy (e.g., title > axis labels) | Creates visual structure |
⚠️ Common Pitfalls
Pitfall | Solution |
---|---|
Labels not showing | Always call plt.show() at the end |
Overlapping labels | Use tight_layout() or adjust pad , rotation |
Legend missing | Make sure label is set in plot() and call legend() |
Using non-matching tick labels | Ensure the number of ticks matches the labels |
Summary
Label Type | Function |
---|---|
X-axis label | plt.xlabel("...") |
Y-axis label | plt.ylabel("...") |
Title | plt.title("...") |
Legend | plt.legend() |
Tick labels | plt.xticks([...]) |
Annotation | plt.annotate(...) |
Conclusion
Labels are the voice of your plot—they explain the context, meaning, and structure of your data. With Matplotlib, adding and customizing labels is simple yet powerful. Mastering these elements will help you create clear, compelling, and professional visualizations.
What’s Next?
-
Learn about subplots and how to label multiple axes
-
Explore interactive labeling with tools like
plotly
-
Use LaTeX-style labels for scientific formatting:
plt.xlabel(r'$x^2 + y^2 = z^2$')