Matplotlib Style Sheets and rcParams
You'll learn how to instantly transform the look of all your plots using Matplotlib's built-in style sheets and global configuration settings.
The Core Idea
Instead of manually styling every plot element each time, Matplotlib lets you apply style sheets—pre-configured visual themes that control colors, fonts, line widths, and more. Think of them like templates or "skins" that change your plot's entire appearance with one line of code.
Additionally, rcParams (run configuration parameters) is a global dictionary that controls Matplotlib's default settings. When you modify rcParams, every plot you create afterward inherits those settings automatically.
Using Style Sheets
Matplotlib comes with built-in style sheets like 'ggplot', 'seaborn', 'fivethirtyeight', and 'bmh'. Apply them with:
import matplotlib.pyplot as plt
plt.style.use('ggplot') # Now all plots use ggplot styling
This single line affects fonts, colors, grid styles, and backgrounds for all subsequent plots. You can also temporarily use a style:
with plt.style.use('seaborn'):
# Plots here use seaborn style
plt.plot([1, 2, 3], [1, 4, 9])
Configuring rcParams
For fine-grained control, modify plt.rcParams directly:
plt.rcParams['figure.figsize'] = (10, 6) # Default figure size
plt.rcParams['font.size'] = 12 # Default font size
plt.rcParams['lines.linewidth'] = 2 # Default line width
These changes apply globally until you reset or change them again.
Key Takeaway: Style sheets provide instant, cohesive visual themes for your plots, while rcParams gives you precise control over default settings—both eliminating repetitive styling code and ensuring consistency across all your visualizations.