Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Bug]: GridSpecFromSubplotSpec displayed incorrectly with layout="constrained" #28891

Copy link
Copy link
@jakelevi1996

Description

@jakelevi1996
Issue body actions

Bug summary

When creating a nested grid of axes using GridSpecFromSubplotSpec (EG by calling axis.get_subplotspec().subgridspec(...)), and plotting a figure using layout="constrained", the nested axes are not displayed correctly. Specifically, the inner grids do not respect the spacing between the outer grids, as shown below.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib.axes
import matplotlib.gridspec
import matplotlib._layoutgrid

def make_demo(
    plot_name,
    outer_space=0.1,
    inner_space=0.1,
    layout=None,
):
    file_name = "%s.png" % plot_name
    print(file_name)

    figure = plt.figure(figsize=[10, 6], layout=layout)
    grid_spec = matplotlib.gridspec.GridSpec(
        nrows=1,
        ncols=2,
        figure=figure,
        wspace=outer_space,
        hspace=outer_space,
        width_ratios=[2, 1],
    )
    axis_array = grid_spec.subplots(squeeze=False)
    a0, a1 = axis_array.flatten().tolist()
    assert isinstance(a0, matplotlib.axes.Axes)
    assert isinstance(a1, matplotlib.axes.Axes)

    for axis in [a0, a1]:
        axis.get_xaxis().set_visible(False)
        axis.get_yaxis().set_visible(False)
        for s in axis.spines.values():
            s.set(color="r", lw=10)

        subplot_spec = axis.get_subplotspec()
        subgrid_spec = subplot_spec.subgridspec(
            nrows=3,
            ncols=3,
            wspace=inner_space,
            hspace=inner_space,
        )
        axis_array = subgrid_spec.subplots(squeeze=False)

    figure.suptitle(plot_name, fontsize=25)
    figure.savefig(file_name)

def fix_layout_grid():
    defaults = matplotlib._layoutgrid.LayoutGrid.__init__.__defaults__
    defaults = list(defaults)
    defaults[2] = True
    defaults = tuple(defaults)
    matplotlib._layoutgrid.LayoutGrid.__init__.__defaults__ = defaults

if __name__ == "__main__":
    make_demo("1_original")
    make_demo("2_more_outer_space", outer_space=0.8)
    make_demo("3_more_inner_space", outer_space=0.8, inner_space=0.8)
    make_demo("4_constrained", layout="constrained")
    make_demo("5_constrained_more_outer_space", layout="constrained", outer_space=0.3)
    make_demo("6_constrained_more_inner_space", layout="constrained", outer_space=0.3, inner_space=0.3)
    fix_layout_grid()
    make_demo("7_fixed_constrained", layout="constrained")
    make_demo("8_fixed_constrained_more_outer_space", layout="constrained", outer_space=0.3)

    # filenames = [
    #     "1_original.png",
    #     "2_more_outer_space.png",
    #     "3_more_inner_space.png",
    #     "4_constrained.png",
    #     "5_constrained_more_outer_space.png",
    #     "6_constrained_more_inner_space.png",
    #     "7_fixed_constrained.png",
    #     "8_fixed_constrained_more_outer_space.png",
    # ]
    # from jutility import util, plotting
    # mp = plotting.MultiPlot(
    #     *[plotting.ImShow(util.load_image(s)) for s in filenames],
    #     colour="grey",
    # )
    # mp.save()

Actual outcome

In images 1-3 below (not constrained layout), the inner grids respond to the spacing between the outer grids. However, when using layout="constrained" in images 4-6 below (before my workaround is applied), the inner grids ignore the spacing between the outer grids.

image

Expected outcome

The inner grids should respond to the spacing between the outer grids (which is the case in images 7-8 after my workaround is applied).

Additional information

I have found a workaround, which doesn't involve modifying the matplotlib installation (included in the fix_layout_grid function above):

defaults = matplotlib._layoutgrid.LayoutGrid.__init__.__defaults__
defaults = list(defaults)
defaults[2] = True
defaults = tuple(defaults)
matplotlib._layoutgrid.LayoutGrid.__init__.__defaults__ = defaults

I have also found 2 possible fixes (either one works, the first is more conservative):

  1. In https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/_constrained_layout.py#L226 , in layoutgrids[rep] = mlayoutgrid.LayoutGrid(...), include parent_inner=True
  2. In https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/_layoutgrid.py#L36 , in LayoutGrid.__init__(...), change parent_inner=False to parent_inner=True

Operating system

Ubuntu

Matplotlib Version

3.9.2

Matplotlib Backend

qtagg

Python version

Python 3.10.12

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.