matplotlib definitely is the first choice for generating plots from Python and has established itself as the de facto standard in scientific plotting. Despite providing a simple interface, it comes with support for even the most exotic graphics. However, the default styling looks dull and could definitely need an update! This article will introduce a custom plotting style that is more pleasing to the eye while it does not distract from the underlaying objective: creating informative and clear graphics.
The presented theme was derived from the excellent work of Huy Nguyen. His
matplotlibrc file is inspired by the default ggplot2 design and was specificly compiled for plots viewed on a computer screen. My take on this topic however is intended to generate plots which are crisp and look beautiful on screen as well as paper.
The following paragraphs are meant to explain the choices made for this style. They do by no means represent a scientific derivation but merely resemble my personal taste – and incorporate feedback from many friends and colleagues.
I am using the
pgf backend which has been introduced to
matplotlib quite lately. It leaves the rendering to XeLaTeX or LuaLaTex and can generate pgf compatible output which can be embedded directly into LaTeX documents.
For printing purposes, I primarily use fonts from the amazing open fonts project Linux Libertine. This of course is debatable – especially for on-screen viewing. For presentations, I mostly switch to Mozilla’s Fira Sans.
pgf backend does not support
plt.show() to show a live version of the plot. I simply work around this issue by writing the output to a file. Alternatively, you can set a different backend programmatically using
The most important component of the color scheme is of course the coloring of the actual data. The following palette is intended to please the eye while at the same time ensuring enough contrast between successive data sets.
This palette was taken from the
matplotlibrc this style is based on. However, the background color was set to white instead of grey. This does not only make sense for printing, but also increases contrast in general. Lines and grid are kept in a light grey tone in order not to distract from the actual content.
To use the
matplotlibrc from the repo, just copy it to either
~/.matplotlib/matplotlibrc depending on your system and package version.