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
Figure.subplots
is a convenient method for creating a grid of subplots with shared axes, with the redundant tick labels and such automatically handled for you. It would be helpful if this method could also be used to help create more complicated layouts, such as ones with a few independent sets of shared axes that you would create with several calls togridspec.GridSpecFromSubplotSpec
.
For example:
importmatplotlib.pyplotaspltimportmatplotlib.gridspecasgridspecimportnumpyasnpfig=plt.figure()gs=gridspec.GridSpec(1,2)##### The following block could be replaced by this proposed function call:#fig.subplots(nrows=2, ncols=2, subplotspec=gs[0], sharex='col', sharey='all',# squeeze=False, gridspec_kw=dict(hspace=0.07))gs0=gridspec.GridSpecFromSubplotSpec(2,2,gs[0],hspace=0.07)ax0_00=fig.add_subplot(gs0[0,0])ax0_01=fig.add_subplot(gs0[0,1],sharey=ax0_00)ax0_10=fig.add_subplot(gs0[1,0],sharex=ax0_00,sharey=ax0_00)ax0_11=fig.add_subplot(gs0[1,1],sharex=ax0_00,sharey=ax0_00)ax0s=np.array([[ax0_00,ax0_01], [ax0_10,ax0_11]])foraxinax0s[:-1].flatten():ax.tick_params(axis='x',which='both',labelbottom=False)foraxinax0s[:,1:].flatten():ax.tick_params(axis='y',which='both',labelleft=False)######### The following block could be replaced by this proposed function call:#fig.subplots(nrows=3, ncols=1, subplotspec=gs[1], sharex='all', sharey='all',# squeeze=False, gridspec_kw=dict(hspace=0.1))gs1=gridspec.GridSpecFromSubplotSpec(3,1,gs[1],hspace=0.1)ax10=fig.add_subplot(gs1[0])ax11=fig.add_subplot(gs1[1],sharex=ax10,sharey=ax10)ax12=fig.add_subplot(gs1[2],sharex=ax10,sharey=ax10)ax1s=np.array([[ax10], [ax11], [ax12]])foraxinax1s[:-1].flatten():ax.tick_params(axis='x',which='both',labelbottom=False)foraxinax1s[:,1:].flatten():ax.tick_params(axis='y',which='both',labelleft=False)####fig.tight_layout()plt.show()
Adding asubplotspec
optional argument toFigure.subplots
, which when supplied would callgridspec.GridSpecFromSubplotSpec
(with the provided SubplotSpec) instead ofgridspec.GridSpec
, would be all that is required.
The exception to this is for constrained layouts, which I'm not familiar with.gridspec.GridSpecFromSubplotSpec
doesn't accept a figure parameter, but seems to inherit that information from the SubplotSpec, and so would rely on passing the figure to the "parent" GridSpec in the first place?