Plot Styling: Text and Labels in Matplotlib
The Story of the Label Artist
Imagine you’re a museum curator. You have beautiful paintings (your data plots), but without labels and descriptions, visitors would be lost! They wouldn’t know what they’re looking at.
Labels and text in Matplotlib work the same way—they’re the helpful signs that guide your viewers through your data story.
X and Y Axis Labels
What Are They?
Axis labels are like street signs for your plot. The X-axis label tells people what’s going along the bottom. The Y-axis label tells people what’s going up the side.
How to Add Them
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.xlabel('Days')
plt.ylabel('Ice Cream Sales')
plt.show()
Make Them Pretty!
You can change the look:
plt.xlabel('Days',
fontsize=14,
color='blue',
fontweight='bold')
plt.ylabel('Sales',
fontsize=14,
color='green')
Think of it like this: You’re writing on a sign, and you get to pick the marker color, size, and thickness!
Plot Titles
The Main Headline
A title is like the name of your painting. It sits at the top and tells everyone: “This is what you’re looking at!”
Adding a Title
plt.plot([1, 2, 3], [10, 20, 15])
plt.title('My First Chart')
plt.show()
Styling Your Title
plt.title('Ice Cream Sales This Week',
fontsize=18,
fontweight='bold',
color='purple',
loc='center') # left, center, right
The loc parameter moves the title:
'left'→ Title sits on the left'center'→ Title sits in the middle (default)'right'→ Title sits on the right
Figure Suptitle
The “Super” Title
When you have multiple charts in one picture, you need a big boss title that covers them all. That’s suptitle (super title)!
graph TD A[Figure Suptitle: My Data Story] --> B[Chart 1: Sales] A --> C[Chart 2: Costs] A --> D[Chart 3: Profit]
How to Add It
fig, axes = plt.subplots(1, 2)
axes[0].plot([1, 2, 3])
axes[0].set_title('Chart A')
axes[1].plot([3, 2, 1])
axes[1].set_title('Chart B')
fig.suptitle('My Two Charts',
fontsize=16,
fontweight='bold')
plt.show()
Key Difference
| Type | What It Does |
|---|---|
title() |
Title for ONE chart |
suptitle() |
Title for the WHOLE figure |
Subplot Titles
Each Chart Gets a Name
When you have multiple small charts (subplots), each one can have its own title!
fig, axes = plt.subplots(2, 2)
axes[0, 0].plot([1, 2, 3])
axes[0, 0].set_title('Top Left')
axes[0, 1].plot([3, 2, 1])
axes[0, 1].set_title('Top Right')
axes[1, 0].plot([1, 3, 2])
axes[1, 0].set_title('Bottom Left')
axes[1, 1].plot([2, 1, 3])
axes[1, 1].set_title('Bottom Right')
plt.tight_layout()
plt.show()
Pro Tip: Use plt.tight_layout() so titles don’t overlap!
Adding Text to Plots
Write Anywhere!
Sometimes you want to write a note right on your chart. Maybe point out something special!
The text() Function
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.text(2, 4, 'Peak!')
plt.show()
This puts the word “Peak!” at position (2, 4) on your chart.
Styled Text
plt.text(2, 4, 'Peak!',
fontsize=12,
color='red',
fontweight='bold',
bbox=dict(boxstyle='round',
facecolor='yellow'))
The bbox adds a colored box around your text—like a sticky note!
Text Positioning
Where Should Your Text Go?
There are two ways to position text:
1. Data Coordinates (Default)
Text goes where your data is:
plt.text(2, 4, 'Here!')
# Goes at x=2, y=4 on your chart
2. Axes Coordinates (0 to 1)
Text goes at a percentage of the chart:
plt.text(0.5, 0.5, 'Center!',
transform=plt.gca().transAxes)
# Goes in the exact middle
graph TD A["#40;0,0#41; = Bottom-Left"] --> B["#40;0.5,0.5#41; = Center"] B --> C["#40;1,1#41; = Top-Right"]
Quick Reference
| Position | Coordinates |
|---|---|
| Bottom-left | (0, 0) |
| Bottom-right | (1, 0) |
| Top-left | (0, 1) |
| Top-right | (1, 1) |
| Center | (0.5, 0.5) |
Text Font Properties
Make Your Text Beautiful!
You control everything about how text looks.
The Main Properties
plt.text(0.5, 0.5, 'Styled Text',
fontsize=16, # How big
fontweight='bold', # How thick
fontstyle='italic', # Slanted?
fontfamily='serif', # Font type
color='navy', # What color
alpha=0.8) # Transparency
Font Size Options
| Size | Value |
|---|---|
| Tiny | 8 |
| Small | 10 |
| Normal | 12 |
| Large | 14 |
| Huge | 20+ |
Font Weight Options
'light'→ Thin letters'normal'→ Regular letters'bold'→ Thick letters'heavy'→ Extra thick!
Font Style Options
'normal'→ Regular'italic'→ Slanted'oblique'→ Also slanted
Font Family Options
'serif'→ Fancy with little feet (Times)'sans-serif'→ Clean and modern (Arial)'monospace'→ All letters same width (Code font)'cursive'→ Handwriting style
All Together Example
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 6))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
# Title with style
plt.title('Sales Report',
fontsize=20,
fontweight='bold',
color='darkblue')
# Axis labels
plt.xlabel('Month',
fontsize=14,
fontfamily='sans-serif')
plt.ylabel('Sales ($)',
fontsize=14,
fontfamily='sans-serif')
# Text annotation
plt.text(2, 4, 'Best Month!',
fontsize=12,
fontweight='bold',
color='green',
bbox=dict(boxstyle='round',
facecolor='lightyellow',
edgecolor='green'))
plt.show()
Quick Summary
graph TD A[Text in Matplotlib] --> B[Axis Labels] A --> C[Titles] A --> D[Annotations] B --> B1[xlabel] B --> B2[ylabel] C --> C1[title - one chart] C --> C2[suptitle - whole figure] C --> C3[set_title - subplots] D --> D1[text - add anywhere] D --> D2[Position - data or axes] D --> D3[Style - size, color, font]
Remember These!
- xlabel/ylabel = Street signs for your axes
- title = Name of one chart
- suptitle = Name of the whole picture
- set_title = Name for each subplot
- text = Notes anywhere on your chart
- transform = Switch between data and axes positions
- fontsize, fontweight, color = Make it pretty!
You’re now a Label Master! Your plots will never be confusing again.