Description
I often have the situation where I have some number of plots I'd like to display that may vary, and I don't want to fiddle around with how many rows and how many columns and such. I think it would be good if matplotlib
provided some way to declare a "grid strategy" (with some built-in strategies to cover most of what people want), such that you could create subplots with something like plt.subplots_from_strategy(n, grid_strategy=SquareGridStrategy)
, and you'd get n
subplots arranged in something as close to a square as possible.
To demonstrate, I've worked up a basic "squarish" grid strategy that creates a symmetrical pattern alternating between rows of length x
and rows of length x-1
to create something close to a square. The code is here (though this is just for demo purposes, I'm not particular about how it's actually implemented), and here is a gallery of demo plots. Some basic examples:
I figure this is a good start, and the 'squarish' grid strategy could be changed to take an aspect ratio defaulting to 1, so that it tries to keep within +/- 1 on either side of the aspect ratio.
I've also implemented in there a related but separate feature, which is the get_gridspec
function, which takes a tuple of length nrows
, where each element is the number of columns in the given row - so the n=7 plot above is represented by (2, 3, 2)
. When creating the gridspec, it center-justifies the evenly-spaced plots rather than having subplots of uneven size like you'd normally get with GridSpecFromSubplotSpec
(see this related SO question). I'll note that it would be no trouble to add right and left justification options for this. If you'd like I can make a separate issue for discussion of this feature (which I think would be useful even without the GridStrategy
stuff above, though the way I've framed the GridStrategy
stuff, I think GridStrategy
relies on this).