customizing
Customizing Matplotlib with style sheets and rcParams
Tips for customizing the properties and default styles of Matplotlib.
Using style sheets
The style package adds support for easy-to-switch plotting “styles” with
the same parameters as a
matplotlib rc file (which is read
at startup to configure matplotlib).
There are a number of pre-defined styles provided by Matplotlib. For
example, there’s a pre-defined style called “ggplot”, which emulates the
aesthetics of ggplot (a popular plotting package for R). To use this style,
just add:
1 | import numpy as np |
To list all available styles, use:
1 | print(plt.style.available) |
Out:
1 | ['seaborn-dark', 'dark_background', 'seaborn-pastel', 'seaborn-colorblind', 'tableau-colorblind10', 'seaborn-notebook', 'seaborn-dark-palette', 'grayscale', 'seaborn-poster', 'seaborn', 'bmh', 'seaborn-talk', 'seaborn-ticks', '_classic_test', 'ggplot', 'seaborn-white', 'classic', 'Solarize_Light2', 'seaborn-paper', 'fast', 'fivethirtyeight', 'seaborn-muted', 'seaborn-whitegrid', 'seaborn-darkgrid', 'seaborn-bright', 'seaborn-deep'] |
Defining your own style
You can create custom styles and use them by calling style.use with the
path or URL to the style sheet. Additionally, if you add your.mplstyle file to mpl_configdir/stylelib, you can reuse
your custom style sheet with a call to style.use(). By defaultmpl_configdir should be ~/.config/matplotlib, but you can check where
yours is with matplotlib.get_configdir(); you may need to create this
directory. You also can change the directory where matplotlib looks for
the stylelib/ folder by setting the MPLCONFIGDIR environment variable,
see matplotlib configuration and cache directory locations.
Note that a custom style sheet in mpl_configdir/stylelib will
override a style sheet defined by matplotlib if the styles have the same name.
For example, you might want to creatempl_configdir/stylelib/presentation.mplstyle with the following:
1 | axes.titlesize : 24 |
Then, when you want to adapt a plot designed for a paper to one that looks
good in a presentation, you can just add:
1 | import matplotlib.pyplot as plt |
Composing styles
Style sheets are designed to be composed together. So you can have a style
sheet that customizes colors and a separate style sheet that alters element
sizes for presentations. These styles can easily be combined by passing
a list of styles:
1 | import matplotlib.pyplot as plt |
Note that styles further to the right will overwrite values that are already
defined by styles on the left.
Temporary styling
If you only want to use a style for a specific block of code but don’t want
to change the global styling, the style package provides a context manager
for limiting your changes to a specific scope. To isolate your styling
changes, you can write something like the following:
1 | with plt.style.context('dark_background'): |

matplotlib rcParams
Dynamic rc settings
You can also dynamically change the default rc settings in a python script or
interactively from the python shell. All of the rc settings are stored in a
dictionary-like variable called matplotlib.rcParams, which is global to
the matplotlib package. rcParams can be modified directly, for example:
1 | mpl.rcParams['lines.linewidth'] = 2 |

Matplotlib also provides a couple of convenience functions for modifying rc
settings. The matplotlib.rc() command can be used to modify multiple
settings in a single group at once, using keyword arguments:
1 | mpl.rc('lines', linewidth=4, color='g') |

The matplotlib.rcdefaults() command will restore the standard matplotlib
default settings.
There is some degree of validation when setting the values of rcParams, seematplotlib.rcsetup for details.
The matplotlibrc file
matplotlib uses matplotlibrc configuration files to customize all kinds
of properties, which we call rc settings or rc parameters. You can control
the defaults of almost every property in matplotlib: figure size and dpi, line
width, color and style, axes, axis and grid properties, text and font
properties and so on. matplotlib looks for matplotlibrc in four
locations, in the following order:
Once a matplotlibrc file has been found, it will not search any of
the other paths.
To display where the currently active matplotlibrc file was
loaded from, one can do the following:
1 | import matplotlib |
See below for a sample matplotlibrc file.
A sample matplotlibrc file
1 | #### MATPLOTLIBRC FORMAT |




