So far, we’ve learned how to create various types of plots — lines, bars, histograms, scatter, pie, and subplots. Now it’s time to take your visualizations to the next level by customizing them.
Matplotlib offers extensive options for styling — from colors, line styles, and fonts to annotations, grids, and themes — helping you make your plots clearer, more professional, and publication-ready.
Why Customize Plots?
Customization improves:
- Readability – Makes data easier to interpret.
- Aesthetics – Makes plots visually appealing.
- Communication – Highlights key insights effectively.
- Branding – You can match company or publication color schemes.
⚙️ Basic Customization Options
1. Changing Line Color, Style, and Width
You can modify the look of lines using arguments like color
, linestyle
, and linewidth
.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.plot(x, y, color='red', linestyle='--', linewidth=2, marker='o', markerfacecolor='blue')
plt.title("Customized Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Options:
color
:'red'
,'green'
,'blue'
, or HEX codes like'#1f77b4'
.linestyle
:'-'
,'--'
,':'
,'-.'
.marker
:'o'
,'s'
,'^'
,'*'
, etc.
2. Adding Titles and Labels
plt.title("Monthly Sales Growth", fontsize=16, fontweight='bold', color='darkblue')
plt.xlabel("Month", fontsize=12)
plt.ylabel("Sales (in $)", fontsize=12)
You can control font size, weight, color, and alignment.
3. Adding Grid Lines
Grids make it easier to read values from the graph.
plt.grid(True, linestyle='--', linewidth=0.5, alpha=0.7)
4. Changing Figure Size
Use plt.figure(figsize=(width, height))
or pass figsize
to subplots()
.
plt.figure(figsize=(8, 5))
plt.plot(x, y, 'g-o')
plt.title("Figure Size Example")
plt.show()
🖌️ Customizing Axes
1. Setting Axis Limits
plt.xlim(0, 10)
plt.ylim(0, 15)
2. Adding Ticks and Custom Labels
plt.xticks([1, 2, 3, 4, 5], ['Jan', 'Feb', 'Mar', 'Apr', 'May'])
plt.yticks(range(0, 12, 2))
Adding Legends
Legends help identify multiple datasets on one plot.
x = [1, 2, 3, 4, 5]
y1 = [2, 3, 5, 7, 11]
y2 = [1, 4, 6, 8, 10]
plt.plot(x, y1, label='Product A', color='blue')
plt.plot(x, y2, label='Product B', color='orange')
plt.legend(loc='upper left', title='Products', fontsize=10)
plt.title("Sales Comparison")
plt.show()
Common legend locations:
'upper left'
,'upper right'
,'lower left'
,'lower right'
,'center'
🖍️ Adding Annotations
Annotations are used to highlight specific points or insights in a chart.
plt.plot(x, y1, 'g-o')
plt.title("Prime Numbers Trend")
plt.annotate('Peak Value', xy=(5, 11), xytext=(3, 12),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
This adds a label pointing to a data point with an arrow.
🧱 Adding Background Color
fig = plt.figure(facecolor='#f0f0f0')
ax = fig.add_subplot()
ax.plot(x, y1, color='teal')
ax.set_facecolor('#ffffff')
plt.title("Custom Background Example")
plt.show()
🎨 Applying Built-in Styles
Matplotlib has several built-in stylesheets that quickly change the look of your plots.
plt.style.use('ggplot') # Other options: 'seaborn', 'classic', 'bmh', 'dark_background'
plt.plot(x, y1, 'r-o')
plt.title("Styled Plot with ggplot Theme")
plt.show()
To see all available styles:
import matplotlib.pyplot as plt
print(plt.style.available)
🌍 Real-World Example – Customizing a Sales Chart
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales_2024 = [300, 400, 500, 600, 700, 800]
sales_2025 = [350, 420, 480, 620, 780, 900]
plt.style.use('seaborn-v0_8-darkgrid')
plt.figure(figsize=(10, 5))
plt.plot(months, sales_2024, marker='o', color='blue', label='2024')
plt.plot(months, sales_2025, marker='s', color='orange', label='2025')
plt.title("Company Sales Growth (2024 vs 2025)", fontsize=16, fontweight='bold')
plt.xlabel("Month", fontsize=12)
plt.ylabel("Sales (in $)", fontsize=12)
plt.legend(loc='upper left')
plt.grid(True, linestyle='--', alpha=0.6)
# Annotate max point
plt.annotate('Highest Sales', xy=('Jun', 900), xytext=('Apr', 950),
arrowprops=dict(facecolor='black', arrowstyle='->'))
plt.tight_layout()
plt.show()
✅ This produces a professional sales trend chart comparing two years with proper labels, styling, and an annotation.
✅ Key Takeaways
- Matplotlib offers deep customization through colors, styles, fonts, grids, and more.
- Use annotations to draw attention to key data points.
- Built-in styles like
ggplot
orseaborn
help quickly enhance visuals. - Consistent customization improves readability and professionalism.