Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
In#14421 there was some concern thatgridspec
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 togs[0, 0]
or a legend just for that gridspec, or a suptitle. Currently we would have to add all that togridspec
. But as@timhoffm pointed out, gridspec is notreally equipped for this sort of thing.
Here instead I propose we create a new base class forFigure
, sayFigureBase
and makeFigure
a subclass of that, andSubFigure
a subclass of that.
FigureBase
contains all the layout-specific functions ofFigure
and all the figure-level artists (figure legends, figure colorbars, suptitle, future supx/ylabel, etc).FigureBase
then gets a new methodsubfigures
andadd_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 doadd_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 ifx0
,y0
etc are in the parent's co-ordinates the subfigure has atransFigure
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.