This notebook presents how to change the style or appearance of matplotlib plots. In additiopn to the rcParams
dictionnary, the matplotlib.style
module provides facilities for style sheets utilisation with matplotlib. Look at this page of the matplotlib documentation to know how it works in details.
Hereafter, I present how to load a style and I give you a style sheet I use for my plots.
In [2]:
import matplotlib
import matplotlib.style as mpl_style
The mpl_style.available
attribute outputs the available styles. Since matplotlib version 2 and higher, you can load for example seaborn styles directly in matplotlib.
In [3]:
mpl_style.available
Out[3]:
If you want to use one specific style, you simply have to load it, for example for the seaborn-dark
style, using:
In [ ]:
mpl_style.use("seaborn-dark")
Or you can also combine serveral styles:
In [ ]:
mpl_style.use("seaborn-dark")
mpl_style.use(["seaborn-ticks", "seaborn-dark"])
There are several ways to change globaly or temporary the plot default parameters.
rcParams
dictionnaryYou can directly modify parameters of the rcParams
dictionnary, for example at the beginning of a notebook, in order to apply a style to all the plots. For example, the following changes the size of the plots, the font size and ask for a grid to be drawn:
plt.rcParams["figure.figsize"] = (10, 6)
plt.rcParams["font.size"] = 20
plt.rcParams["axes.grid"] = True
You can download from here the matplotlibrc
file. This file contains all the style options of the plots. Modify the file as you whish in order to change the default parameters. Look at this page in order to know where you have to save the file.
If you want the parameters to be limited to a specific location, copy the matplotlibrc
file in your working directory. Then, only the plots you will create in this directory will be affected by these parameters.
A matplotlib style sheet has the same format and syntax as the matplotlibrc
file. You simply have to put in this file the specific parameters you need. Suppose that at the beginning of all your jupyter notebook, you always modify the same keys of the rcParams
dictionnary. You can consider to write these parameters in a style sheet and load that parameters at the beginning of the notebook.
In order to know where you have to save your style sheet, you can run the following command.
In [4]:
matplotlib.get_configdir()
Out[4]:
The style sheets have to be in a subdirectory called stylelib
of the above location. You have to save the style sheets with names such as my_style.mplstyle
.
Hereafter is the style sheet I wrote to produce figures for scientific publications. This is not a perfect style sheet but a not too bad working example. Just save it put your own touch.
matplotlibrc
#### MATPLOTLIBRC FORMAT
## matplotlib configuration for plots for publications.
#### FIGURE
figure.figsize : 11.67, 8.27
savefig.dpi : 300
savefig.bbox : tight
#### FONT
font.size : 24
font.family : serif
#### LaTeX
mathtext.default : regular
#### AXES
axes.linewidth : 2
axes.grid : True
#### TICKS
xtick.direction : in
xtick.top : True
xtick.major.width : 2
xtick.major.size : 10
xtick.minor.visible : True
xtick.minor.width : 2
xtick.minor.size : 5
ytick.direction : in
ytick.right : True
ytick.major.width : 2
ytick.major.size : 10
ytick.minor.visible : True
ytick.minor.width : 2
ytick.minor.size : 5
In [5]:
%matplotlib inline
In [6]:
import matplotlib.pyplot as plt
import numpy as np
Plot a simple linear function as if it was a model of some experimental data.
In [25]:
x = np.random.uniform(0, 10, 30)
x.sort()
yt = 2 * x + 1
y = yt + np.random.normal(loc=0, scale=2, size=y.size)
plt.plot(x, yt, label="model")
plt.plot(x, y, "o")
plt.xlabel("x values (unit)")
plt.ylabel("y values (unit)")
plt.title("A plot")
Out[25]:
Now, supposed you have saved you style sheet with as publi.mplstyle
in the right directory. You can load the style and draw the plot:
In [27]:
with plt.style.context(('publi')):
plt.plot(x, yt, label="model")
plt.plot(x, y, "o")
plt.xlabel("x values (unit)")
plt.ylabel("y values (unit)")
plt.title("A plot")