Matplotlib Labels
Add titles, axis labels, and legends to make your plots informative
🏷️ Labeling Your Plots
Labels are essential for making your plots understandable. They provide context and help viewers interpret your data correctly.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title('Sales Growth')
plt.xlabel('Quarter')
plt.ylabel('Sales ($1000)')
plt.show()
Essential Plot Labels
Every good plot needs these key labels for clarity:
Title
Main heading describing the plot
X-axis Label
Describes horizontal axis data
Y-axis Label
Describes vertical axis data
Legend
Explains different data series
🔹 Basic Labels
Add essential labels to make your plot informative
import matplotlib.pyplot as plt
# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [20, 35, 30, 35, 27]
# Create plot with labels
plt.plot(months, sales, marker='o')
# Add labels
plt.title('Monthly Sales Report')
plt.xlabel('Month')
plt.ylabel('Sales (in thousands)')
plt.show()
🔹 Styling Labels
Customize the appearance of your labels
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 8, 7]
plt.plot(x, y, 'bo-')
# Styled labels
plt.title('Styled Plot Title',
fontsize=16,
fontweight='bold',
color='darkblue')
plt.xlabel('X-axis Label',
fontsize=12,
color='red')
plt.ylabel('Y-axis Label',
fontsize=12,
color='green')
plt.show()
# More styling options
plt.figure()
plt.plot(x, y, 'ro-')
plt.title('Advanced Styling',
fontsize=18,
fontweight='bold',
fontfamily='serif',
color='purple',
pad=20) # Add space above title
plt.xlabel('Time (hours)', fontsize=14, style='italic')
plt.ylabel('Temperature (°C)', fontsize=14, style='italic')
plt.show()
🔹 Legends
Add legends to explain multiple data series
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 1, 5, 3]
y2 = [3, 1, 4, 2, 5]
y3 = [1, 3, 2, 4, 1]
# Plot multiple lines with labels
plt.plot(x, y1, 'r-', label='Product A')
plt.plot(x, y2, 'g--', label='Product B')
plt.plot(x, y3, 'b:', label='Product C')
# Add legend
plt.legend()
plt.title('Product Sales Comparison')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
# Custom legend positioning
plt.figure()
plt.plot(x, y1, 'r-', label='Series 1')
plt.plot(x, y2, 'g--', label='Series 2')
# Legend with custom position
plt.legend(loc='upper right') # or 'lower left', 'center', etc.
plt.title('Custom Legend Position')
plt.show()
🔹 Text Annotations
Add custom text anywhere on your plot
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 8, 7]
plt.plot(x, y, 'bo-')
# Add text annotation
plt.text(3, 8.5, 'Peak Value',
fontsize=12,
ha='center', # horizontal alignment
va='bottom') # vertical alignment
# Annotate specific point
plt.annotate('Highest Point',
xy=(4, 8), # point to annotate
xytext=(4.5, 6), # text position
arrowprops=dict(arrowstyle='->', color='red'))
plt.title('Sales Data with Annotations')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
🔹 Complete Example
A well-labeled plot with all elements
import matplotlib.pyplot as plt
# Data
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
revenue_2022 = [100, 120, 140, 160]
revenue_2023 = [110, 130, 150, 180]
# Create figure with custom size
plt.figure(figsize=(10, 6))
# Plot data
plt.plot(quarters, revenue_2022, 'o-', linewidth=2, label='2022')
plt.plot(quarters, revenue_2023, 's--', linewidth=2, label='2023')
# Add comprehensive labels
plt.title('Quarterly Revenue Comparison\n2022 vs 2023',
fontsize=16, fontweight='bold', pad=20)
plt.xlabel('Quarter', fontsize=12, fontweight='bold')
plt.ylabel('Revenue ($ millions)', fontsize=12, fontweight='bold')
# Customize legend
plt.legend(title='Year',
title_fontsize=12,
fontsize=11,
loc='upper left',
frameon=True,
shadow=True)
# Add grid for better readability
plt.grid(True, alpha=0.3)
# Tight layout to prevent label cutoff
plt.tight_layout()
plt.show()