8000 FR: Figure.subplots add optional SubplotSpec parameter · Issue #11288 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
FR: Figure.subplots add optional SubplotSpec parameter #11288
Closed
@attoblarg

Description

@attoblarg

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 to gridspec.GridSpecFromSubplotSpec.

For example:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

fig = 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]])

for ax in ax0s[:-1].flatten():
    ax.tick_params(axis='x', which='both', labelbottom=False)
for ax in ax0s[:,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]])

for ax in ax1s[:-1].flatten():
    ax.tick_params(axis='x', which='both', labelbottom=False)
for ax in ax1s[:,1:].flatten():
    ax.tick_params(axis='y', which='both', labelleft=False)
####

fig.tight_layout()

plt.show()

figure_1

Adding a subplotspec optional argument to Figure.subplots, which when supplied would call gridspec.GridSpecFromSubplotSpec (with the provided SubplotSpec) instead of gridspec.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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: geometry managerLayoutEngine, Constrained layout, Tight layout

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0