Basic Plot Styling and rcParams
You'll learn how to apply consistent, professional styling across all your Matplotlib visualizations using global configuration.
The Problem: Repetitive Styling
By now you've customized individual plots—changing colors, line widths, fonts. But imagine needing to create a dozen charts with the same look. Manually styling each one is tedious and error-prone. What if you could set a "house style" once and have all future plots automatically match?
Two Global Styling Solutions
1. Built-in Styles with plt.style.use()
Matplotlib ships with pre-designed style sheets—think of them as themes. Apply one with a single line:
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8') # Clean, professional look
# or try: 'ggplot', 'fivethirtyeight', 'bmh', 'dark_background'
# Now every plot uses this style automatically
plt.plot([1, 2, 3], [1, 4, 9])
plt.title("This uses seaborn styling")
plt.show()
2. Fine-Tuned Control with rcParams
rcParams (runtime configuration parameters) is a dictionary controlling Matplotlib's default behavior. Modify specific aspects while keeping everything else standard:
plt.rcParams['lines.linewidth'] = 2.5
plt.rcParams['font.size'] = 12
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['figure.figsize'] = (10, 6)
# All subsequent plots inherit these defaults
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
Think of rcParams as your visualization preferences file—set it once at the beginning of your notebook, and every plot respects those choices.
When to Use Each
plt.style.use(): Quick, cohesive themes for presentations or reportsrcParams: Precise control over individual parameters when themes don't quite fit
Key Takeaway: Use plt.style.use() to apply pre-built themes or rcParams to customize global plotting defaults, ensuring consistent styling across all visualizations without repetitive code.