name = '2016-01-29-matplotlib-styles'
title = 'Matplotlib styles'
tags = 'matplotlib, publications'
author = 'Denis Sergeev'
from nb_tools import connect_notebook_to_post
from IPython.core.display import HTML
html = connect_notebook_to_post(name, title, tags, author)
One of the main applications of Python among the members of our group is, admittedly, visualising data in pulication-quality figures. This was the topic for today's meeting and we will certainly come back to it in the future.
But first, go and get some inspiration for the modern reproducible research based on Jupyter Notebooks: Reproducible academic publications. If not convinced, read this Nature paper.
Matplotlib package has a good enough set of default settings, but almost always you have to fiddle with some of them to get a high-quality figure. Usually it is done explicitly when creating a plot. For example, to set a title with a specific font size, you would use this syntax:
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
plt.title('This is my title', fontsize=20)
<matplotlib.text.Text at 0x7f5a715430f0>
However, it is often useful to set the same title size for every figure you produce. One of the ways to define it is changing the default matplotlib parameters before runnning a script. These defaults, called rcParams
, are stored in a dictionary-like variable called matplotlib.rcParams
, which is global to the matplotlib package. Its location can be found by this command:
mpl.matplotlib_fname()
rcParams
can be modified dynamically in a python script or shell. For example, to set the title font size equal to 20 for every figure in a script, you can put this line in the beginning of the script:
mpl.rcParams['axes.titlesize'] = 20
More about rcParams
can be found on the official website.
In the latest version of matplotlib the process of customizing the look your figures became even easier with the styles
submodule. This package allows you to create easy-to-switch matplotlibrc
-like style-sheets. A list of pre-defined style-sheets, supplied with matplotlib, can be obtained like this:
plt.style.available
['seaborn-bright', 'seaborn-whitegrid', 'grayscale', 'seaborn-poster', 'seaborn-darkgrid', 'seaborn-notebook', 'seaborn-dark-palette', 'seaborn-paper', 'seaborn-pastel', 'fivethirtyeight', 'seaborn-dark', 'seaborn-muted', 'seaborn-ticks', 'seaborn-white', 'bmh', 'seaborn-talk', 'ggplot', 'seaborn-deep', 'seaborn-colorblind', 'classic', 'dark_background', 'awesome']
You can switch any of them using the command, which will set new set of rcParams
for the current script/shell/notebook. Take a look at some of them here: Matplotlib Style Gallery.
plt.style.use('bmh')
def sample_plot():
plt.plot(range(20))
plt.plot([i**2 for i in range(10)])
sample_plot()
rcParams
¶Ok, but how do you restore the default settings without restarting a kernel?
According to this answer on StackOverflow, you should first save the default settings in a dictionary (before using any styles):
inline_rc = dict(mpl.rcParams)
Then, when you are fed up with style-sheets and just want to go back to good old defaults, you can run this command:
mpl.rcParams.update(inline_rc)
Another solution is to use a context manager. In other words, you can use a style temporary for a specified figure, without changing global settings:
with plt.style.context('dark_background'):
sample_plot()
HTML(html)
This post was written as an IPython (Jupyter) notebook. You can view or download it using nbviewer.