Description
In #14421 there was some concern that gridspec
was being overloaded and that something else should be doing that layout.
I've mocked the following up, and it works quite well so far. Getting past mockup will be a fair bit of work, so wanted some comment.
For sub-grids, currently we would do:
fig = plt.figure()
gs = fig.add_gridspec(2, 1)
axs0 = gs[0, 0].subgridspec(3, 2).subplots()
axs1 = gs[1, 0].subgridspec(3, 2).subplots()
Which is fine, for what it is. But imagine you want to have a colorbar that only pertains to gs[0, 0]
or a legend just for that gridspec, or a suptitle. Currently we would have to add all that to gridspec
. But as @timhoffm pointed out, gridspec is not really equipped for this sort of thing.
Here instead I propose we create a new base class for Figure
, say FigureBase
and make Figure
a subclass of that, and SubFigure
a subclass of that.
FigureBase
contains all the layout-specific functions of Figure
and all the figure-level artists (figure legends, figure colorbars, suptitle, future supx/ylabel, etc). FigureBase
then gets a new method subfigures
and add_subfigure
, which give a virtual figure within the current figure.
Now, instead of the above, we write:
fig = plt.figure()
sfigs = fig.subfigures(2, 1)
axs0 = sfigs[0, 0].subplots(3, 2)
axs1 = sfigs[1, 0].subplots(2, 2)
You can also do add_subfigure
using subplotspecs
fig = plt.figure()
gs = fig.add_gridspec(2, 2)
sfig = fig.add_subfigure(gs[:, 1])
axs0 = sfig.subplots(3, 2)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[1, 0])
plt.show()
The subfigs should be API-wise the same as a normal figure. And a subfig can then create subfigs. The subfigs keep track of their parent fig, and the parent keeps track of its subfigs, so traversing the tree is straight forward.
I think this is cleaner than overloading gridspec. Its semi-major surgery on Figure, but I've already implemented the above without too much pain. The layout is all achieved relatively painlessly by having a second transform with the parent so that if x0
, y0
etc are in the parent's co-ordinates the subfigure has a transFigure
given by:
self.bbox_relative = Bbox.from_bounds(x0, y0, widthf, heightf)
self.bbox = TransformedBbox(self.bbox_relative,
self._parent.transFigure)
self.transFigure = BboxTransformTo(self.bbox)
Anyway, wanted to throw this out there for comment or thoughts. This seems a potentially cleaner way to arrange hierarchies of subplots.