A new plot theme for Matplotlib — Gadfly

Jonny Brooks-Bartlett
Towards Data Science
3 min readSep 24, 2018

--

Sample plots from the new plotting theme inspired by the Gadfly plotting package for the Julia programming language

Introduction

I’ve made a plotting theme for Matplotlib that’s inspired by the default plotting theme used in Gadfly for the Julia programming language. Typically I’d just write the code in Julia, which is what I’ve done for many of my previous blog posts. However, since upgrading to Julia v1.0, I’ve been unable to import Gadfly, which means no more pretty Gadfly plots. So I said to myself “Jonny, it’s time to just create the theme yourself”. And I did!

(If you want to find out how to use this theme then skip to the last section)

More (pretty) plots

KDE plots

Left: Two 1-dimensional kernel density estimate (KDE) plots. Right: A 2D KDE plot.

Line plot

All the lines. Code to generate the data shamelessly stolen from Jake VanderPlas’s article on how to create Simple line plots in Python.

Scatter plot

Scatter plot. Code to generate the plot shamelessly stolen from this article on how to create scatter plots in Python.

Plotting with Seaborn

When plotting using the Seaborn, the default aesthetics change a little. I haven’t worked out how to change this exactly but I’m not too fussed about it. For example, boxplots are very different (see below)

Code for creating the boxplots below
Left: Standard matplotlib boxplot function. Right: Boxplot using Seaborn

When plotting withsns.lmplot the legend is plotted outside of the axes and does not have a border (below). It also draw the left and bottom spines by default too.

Scatter plot made with maplotlib’s scatterplot function
Scatter plot made with seaborn’s lmplot function

How to get the theme

This theme isn’t installed in the set of themes that come with matplotlib. Therefore, if you want to use the theme you have a few options. I’ll list a couple of the options here but you can learn about all of the ways by reading the matplotlib documentation on Customizing Matplotlib with style sheets and rcParams.

Option 1

Copy the matplotlibrc style sheet contents at the end of this blog post and save it in the same directory as the script from which you want to do your plotting. Save it under the name matplotlibrc . Then it should just work.

Option 2

Again, copy the matplotlibrc style sheet contents at the end of this blog post but this time save as in mpl_configdir/stylelib as <style-name>.mplstyle. You can find out where mpl_configdir is with matplotlib.get_configdir(). I had to create the stylelib directory because it wasn’t already in the mpl_configdir directory.

Once you’ve saved the file in the right directory you can then set the theme with

import matplotlib.pyplot as plt
plt.style.use(<style-name>)

So I saved the file as gadfly.mplstyle and I can set the theme with plt.style.use('gadfly')

The gadfly theme matplotlibrc file

--

--

Data scientist at Deliveroo, public speaker, science communicator, mathematician and sports enthusiast.