🎨 Plot Styling: Style Sheets and Themes in Matplotlib
The Wardrobe Analogy
Imagine your plot is like a person getting ready for an event. Without any styling, they’re wearing plain white clothes — functional but boring! Style sheets are like complete outfits you can put on with one click. rcParams are like adjusting individual pieces — changing the hat, the shoes, or the belt.
🌟 Style Sheets Basics
What Are Style Sheets?
Style sheets are pre-made collections of settings that change how your plot looks — colors, fonts, line thickness, background, and more.
Think of it like this:
- You want to decorate a room
- Instead of picking each item one by one…
- You pick a “theme” and everything matches instantly!
How to Use a Style Sheet
import matplotlib.pyplot as plt
# Apply a style sheet
plt.style.use('ggplot')
# Now make your plot
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
That’s it! One line changes everything.
Temporary Styling (Just for One Plot)
What if you want a style only for ONE plot?
with plt.style.context('dark_background'):
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
# After this block, style goes back to normal!
It’s like borrowing a costume for one party, then returning it.
📚 Built-in Style Sheets
Matplotlib comes with many free outfits already in the closet!
See All Available Styles
import matplotlib.pyplot as plt
print(plt.style.available)
Popular Built-in Styles
| Style Name | Looks Like |
|---|---|
ggplot |
R’s famous ggplot2 look |
seaborn |
Clean, modern, statistical |
dark_background |
White lines on dark |
bmh |
Bayesian Methods style |
fivethirtyeight |
News website look |
grayscale |
Black & white only |
classic |
Old Matplotlib look |
Example: Comparing Styles
import matplotlib.pyplot as plt
styles = ['ggplot', 'seaborn', 'bmh']
data = [1, 4, 9, 16, 25]
for style in styles:
with plt.style.context(style):
plt.plot(data)
plt.title(f'Style: {style}')
plt.show()
Each style gives your data a completely different personality!
✨ Custom Style Sheets
Why Make Your Own?
Built-in styles are great, but what if you want:
- Your company’s colors?
- Your favorite font?
- A special look for your reports?
Create your own style sheet!
Step 1: Create a .mplstyle File
Make a text file called my_style.mplstyle:
# My Custom Style
axes.facecolor: #f0f0f0
axes.edgecolor: #333333
axes.labelcolor: #333333
axes.prop_cycle: cycler('color', ['#e41a1c', '#377eb8', '#4daf4a'])
lines.linewidth: 2
font.size: 12
figure.facecolor: white
Step 2: Use Your Custom Style
Option A: Put the file in Matplotlib’s style folder:
import matplotlib
print(matplotlib.get_configdir())
# Put your file in: [configdir]/stylelib/
Option B: Use the full file path:
plt.style.use('./my_style.mplstyle')
What Can You Customize?
Almost everything!
- Colors: Background, lines, text
- Fonts: Family, size, weight
- Lines: Width, markers, dashes
- Grid: Show/hide, color, style
- Figure: Size, resolution
⚙️ rcParams Configuration
What is rcParams?
rcParams = Runtime Configuration Parameters
It’s a giant dictionary that controls EVERY setting in Matplotlib. Style sheets are just organized collections of rcParams!
View Current Settings
import matplotlib as mpl
# See one setting
print(mpl.rcParams['lines.linewidth'])
# See all settings (there are hundreds!)
print(mpl.rcParams.keys())
Change Settings Directly
import matplotlib as mpl
# Change one setting
mpl.rcParams['lines.linewidth'] = 3
mpl.rcParams['font.size'] = 14
# Now all plots use these settings!
Common rcParams to Know
# Line appearance
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['lines.linestyle'] = '-'
mpl.rcParams['lines.marker'] = 'o'
# Font settings
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.size'] = 12
# Axes settings
mpl.rcParams['axes.grid'] = True
mpl.rcParams['axes.facecolor'] = '#f5f5f5'
# Figure settings
mpl.rcParams['figure.figsize'] = [8, 6]
mpl.rcParams['figure.dpi'] = 100
Reset to Defaults
Made a mess? Start fresh!
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
rcParams vs Style Sheets
graph TD A[Want to style your plot?] --> B{How many settings?} B -->|Just 1-2 settings| C[Use rcParams directly] B -->|Many settings| D{Reuse often?} D -->|Yes| E[Create custom .mplstyle] D -->|No| F[Use with block + rcParams]
🎯 Quick Reference
| Task | Code |
|---|---|
| Apply a style | plt.style.use('ggplot') |
| Temporary style | with plt.style.context('dark_background'): |
| List all styles | print(plt.style.available) |
| Change one param | mpl.rcParams['font.size'] = 14 |
| Reset everything | mpl.rcParams.update(mpl.rcParamsDefault) |
🚀 Pro Tips
-
Combine styles:
plt.style.use(['seaborn', 'my_custom'])— later styles override earlier ones. -
Check before changing:
print(mpl.rcParams['lines.linewidth']) -
Use context for experiments:
with plt.style.context('bmh'): # Try something new here pass # Original style is back! -
Save your favorites: Create a
.mplstylefile for styles you use often.
đź’ˇ Remember
- Style sheets = Complete outfits (one line, many changes)
- Built-in styles = Free wardrobe options
- Custom styles = Design your own look
- rcParams = Fine-tune individual settings
You now have the power to make your plots look exactly how you want them! 🎨