How to Create Custom Style in Matplotlib for Beautiful Plots
You can create a custom style in
matplotlib by defining your style settings in a dictionary or a .mplstyle file and then applying it with plt.style.use(). This lets you control colors, fonts, line widths, and more to make your plots look unique.Syntax
To create a custom style, define a dictionary with style parameters or write a .mplstyle file. Then apply it using plt.style.use().
plt.style.use('your_style'): Applies a style by name or file path.- Style parameters include colors, font sizes, line widths, grid styles, etc.
- Custom styles can be saved as .mplstyle files in your matplotlib config directory or loaded directly from a path.
python
import matplotlib.pyplot as plt # Define a custom style as a dictionary custom_style = { 'axes.titlesize': 'large', 'axes.labelsize': 'medium', 'lines.linewidth': 2, 'lines.color': 'red', 'grid.color': 'gray', 'grid.linestyle': '--', 'grid.linewidth': 0.5, 'font.family': 'serif' } # Apply the custom style temporarily with plt.style.context(custom_style): plt.plot([1, 2, 3], [4, 5, 6]) plt.title('Custom Style Plot') plt.grid(True) plt.show()
Output
A line plot with red thick lines, serif font, and dashed gray grid lines.
Example
This example shows how to create a custom style file and use it to style a plot. The style changes the background color, grid, and font.
python
import matplotlib.pyplot as plt import os # Create a custom style file content style_content = ''' axes.facecolor : #EAEAF2 grid.color : #FFFFFF grid.linestyle : - grid.linewidth : 1.5 font.size : 12 lines.linewidth : 2 lines.color : #007ACC ''' # Save the style file style_path = 'custom_style.mplstyle' with open(style_path, 'w') as f: f.write(style_content) # Use the custom style plt.style.use(style_path) # Plot with custom style plt.plot([1, 2, 3, 4], [10, 20, 25, 30]) plt.title('Plot with Custom Style File') plt.grid(True) plt.show() # Clean up the style file os.remove(style_path)
Output
A line plot with a light gray background, white grid lines, blue thick lines, and font size 12.
Common Pitfalls
Common mistakes when creating custom styles include:
- Not using valid style keys or values, which causes errors or no effect.
- Forgetting to apply the style with
plt.style.use()orplt.style.context(). - Overwriting default styles without knowing the exact parameter names.
- Not saving the .mplstyle file in the correct directory or providing the full path.
Always check the Matplotlib style sheet documentation for valid keys.
python
import matplotlib.pyplot as plt # Wrong: Using an invalid key wrong_style = {'axes.title_size': 'large'} # should be 'axes.titlesize' try: plt.style.use(wrong_style) except Exception as e: print(f'Error: {e}') # Right: Correct key correct_style = {'axes.titlesize': 'large'} plt.style.use(correct_style) plt.plot([1, 2, 3], [4, 5, 6]) plt.title('Correct Style Applied') plt.show()
Output
Error: style specification must be a string or list of strings
A plot with title font size set to large.
Quick Reference
Here are some common style parameters you can customize:
| Parameter | Description | Example Value |
|---|---|---|
| axes.titlesize | Size of the axes title | 'large', 14 |
| axes.labelsize | Size of the x and y labels | 'medium', 12 |
| lines.linewidth | Width of plot lines | 2, 3.5 |
| lines.color | Color of plot lines | 'red', '#007ACC' |
| grid.color | Color of grid lines | 'gray', '#CCCCCC' |
| grid.linestyle | Style of grid lines | '--', '-.' |
| font.family | Font family for text | 'serif', 'sans-serif' |
| figure.facecolor | Background color of the figure | 'white', '#F0F0F0' |
Key Takeaways
Create custom styles by defining style parameters in a dictionary or .mplstyle file.
Apply custom styles using plt.style.use() or plt.style.context() for temporary use.
Use valid style keys exactly as documented to avoid errors or no effect.
Save .mplstyle files in the matplotlib config directory or load them by full path.
Test your style changes on simple plots to see their visual effect clearly.