Let's do a matplotlibrc

2019-01-19 tips-and-tricks matplotlib

After some misadventures with matplotlib (misadventures due to the famous display errors, if you really need to know, if you don’t configure correctly your matplolib you can generate empty figures… see the backend section later :o) I talked about the joy of having a matplotlibrc and how life is easier with one.

What is a matplotlibrc?

It’s just a configuration file for matplotlib (just like your bashrc) that avoids to play with the options dynamically, and to keep them sitting close to you for your next use.

You’ll admit that for backends issues (the source you use in matploblib, more details here), on servers without X servers and to have a consistent style for every figure in your thesis, it’s quite useful!

Where do I keep my matplotlibrc?

It’s quite simple, matplotlib is going to look at 4 different places when it loads:

  1. in the matplotlibrc file in the current directory
  2. in $MATPLOTLIB/matplotlibrc
  3. in .config/matplotlib/matplotlibrc (or .matplotlib/matplotlibrc, depending on your OS)
  4. in /etc/matplotlibrc where it will be rewritten everytime you reinstall matplotlib

How do I know which file is loaded?

Just run the following lines in your iPython3 shell:

import matplotlib
matplotlib.matplotlib_fname()

What do I put in my matplotlibrc?

To be short (yes, thank you very much, I know how to keep it short from times to times ^^): every configuration of matplotlib you wish to keep from one time to another.

I do think about the backend you use.

To do that, just add the following lines in your matplotlibrc:

# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo 
# MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG # Template.
# You can also deploy your own backend outside of matplotlib by
# referring to the module name (which must be in the PYTHONPATH) as
# 'module://my_backend'.
backend : Agg

And, so that the backend finds automatically another solution in case of conflicts:

# if you are running pyplot inside a GUI and your backend choice
# conflicts, we will automatically try to find a compatible one for
# you if backend_fallback is True
backend_fallback: True

Some coolish options:

interactive : False
toolbar : toolbar2 # None | toolbar2 ("classic" is deprecated)
timezone : Europe # a pytz timezone string, e.g., US/Central or Europe/Paris

Where the magic begins…

Yes, everything before is great, but I promised consistent figures throughout your thesis!

In fact, you can specify everything in your matplotlibrc: from how the lines look in boxplots, through the fonts and the use of LaTeX.

Rather than giving you a list that long, I suggest you check this wonderful git repository. You’ll find demos of several matplotlibrc: some R-like, some ggplot-like,… and since we are in the middle of the wonderful world of configuration files, please know that a lot of those exist (some are even listed in that git repository).

Here you go, go to your favorite text editor and go create your own matplotlibrc.