A new plot theme for Matplotlib — Gadfly
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
Line plot
Scatter plot
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)
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.
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')