How to Use Styles in Matplotlib for Better Plots
In
matplotlib, you can use styles by calling plt.style.use() with a style name or path to a style file. Styles change the appearance of plots, including colors, fonts, and grid lines, making your charts look better with minimal effort.Syntax
To apply a style in Matplotlib, use the function plt.style.use(). You can pass a string with the style name or a list of style names to combine multiple styles.
plt.style.use('style_name'): Applies a single style.plt.style.use(['style1', 'style2']): Combines multiple styles, where later styles override earlier ones.
Common built-in styles include 'ggplot', 'seaborn', 'bmh', and 'classic'.
python
import matplotlib.pyplot as plt plt.style.use('ggplot') # Apply the 'ggplot' style # Now any plot will use this style plt.plot([1, 2, 3], [4, 5, 6]) plt.title('Plot with ggplot style') plt.show()
Example
This example shows how to switch between different styles and how the plot appearance changes. It plots the same data three times with different styles.
python
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) styles = ['classic', 'ggplot', 'seaborn-darkgrid'] for style in styles: plt.style.use(style) plt.plot(x, y) plt.title(f'Sine Wave with {style} style') plt.show()
Output
Three separate plots appear, each showing a sine wave with different colors, grid styles, and fonts according to the selected style.
Common Pitfalls
Common mistakes when using styles include:
- Not calling
plt.style.use()before plotting, so the style does not apply. - Using a style name that does not exist, which causes an error.
- Expecting styles to change plot data or layout beyond appearance (styles only affect visual style).
Also, styles are global and affect all plots after the call, so reset to default with plt.style.use('default') if needed.
python
import matplotlib.pyplot as plt # Wrong: style applied after plotting plt.plot([1, 2, 3], [4, 5, 6]) plt.style.use('ggplot') # This won't affect the above plot plt.show() # Right: style applied before plotting plt.style.use('ggplot') plt.plot([1, 2, 3], [4, 5, 6]) plt.show()
Output
First plot uses default style, second plot uses ggplot style with different colors and grid.
Quick Reference
| Command | Description |
|---|---|
| plt.style.use('style_name') | Apply a built-in or custom style by name |
| plt.style.use(['style1', 'style2']) | Combine multiple styles, later ones override earlier |
| plt.style.use('default') | Reset to Matplotlib's default style |
| plt.style.available | List all available built-in styles |
| plt.style.context('style_name') | Temporarily apply a style within a with-block |
Key Takeaways
Use plt.style.use('style_name') before plotting to apply a style.
Matplotlib has many built-in styles like 'ggplot' and 'seaborn' for quick styling.
Styles affect only the look, not the data or plot structure.
Reset styles with plt.style.use('default') if needed.
Use plt.style.context() to apply styles temporarily.