Rules for better figures
I found the defaults of Matplotlib fairly reasonable. Is fine for many purposes. But it kinda breaks many of the Ten simple rules for better figures1. Just to mention one, the Rule 5: Do not trust the defaults. The main reason is to respect the other rules. In general, not following rule 5 works against Rule 1: Know Your Audience. In some other cases, it breaks Rule 8: Avoid “Chartjunk”. For example, if your data involves only two variables, you should have only two axes.
How I go about it
I have a custom style for plots that is more to my taste. The cover of this blog post has a comparison between two examples of the default Matplotlib style and my config.
Let’s take a closer look. The default Matplotlib style looks like this:
If we apply my config, we can have it look like this:
Let’s learn how to set this style up.
Method A: The config file way
If you want more information about the Matplotlib config file, go here.
- Set the style: This style is achieved with this config file
|
|
- Save the config file on step 1 as
myStyle.mplstyle
. Once you load it all plots ran after it will have the custom style. You can load it with:
|
|
Alternative step 2
-
- For a local config: Save the file as
matplotlibrc
in the current directory. - For a global config: Save the file as
matplotlibrc
to the directory shown bymatplotlib.matplotlib_fname()
:
- For a local config: Save the file as
|
|
Method B: The dictionary way
There are other alternatives to achieve this style. The plt.style.use
function accepts preferences as a dictionary:
|
|
-
Rougier NP, Droettboom M, Bourne PE (2014) Ten Simple Rules for Better Figures. PLoS Comput Biol 10(9): e1003833. https://doi.org/10.1371/journal.pcbi.1003833 ↩︎