Plthacks is a Python package that wraps Matplotlib to make common (tedius) tasks faster, this post is about creating a grid of an arbitrary number of plots from an ordered dictionary.
I reguarly find myself wanting to display multiple fields. I find it frustrating how many lines you need in Matplotlib to grid plots such that they have things like:
when sharing an axis removing ticks and labels where appropriate;
displaying one colourbar for each subplot or a global colourbar for all plots;
adjusting subplot size according to the above.
Plthacks.Grid Class is an attempt to make this much faster. Let’s see an example.
‘Install’ the package somewhere you’ll import it..
To start, we’ll just create some data and store it in an ordered dictionary called plotdict. We also optionally specify each subplot’s dimension labels and colorbars, called dimlab and colorbars respectively.
We’ll now show 10 different usages of the Grid class so you get the idea of what it can do.
Results in:
Neat right? So you’ll notice we’ve opted for 4 rows and 3 columns with 12 plots but the grid can be whatever we like (I hope!).
We can do a few other things too, such as:
share x-axis/share y-axis
include/exclude dimension labels
include/exclude local or global colourbar(s)
have different colourmaps for each subplot
These options are described in the Grid docstring:
You’ll notice in the above example we used the optional dimlabels and sepcbar. Here’s some more examples.
Of course, it’s super hard to create a Class that will work in all instances. I’ve written it with the idea that one could just copy the whole Class in main.py and hack it, if the set up described here is inadequate. Similarly, the Class will return all the matplotlib axis (see self.paxis dictionary). Hopefully it’s a useful starting point though. Pull requests most welcome!