Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

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

@jakelevi1996

Description

@jakelevi1996

Bug summary

When creating a nested grid of axes usingGridSpecFromSubplotSpec (EG by callingaxis.get_subplotspec().subgridspec(...)), and plotting a figure usinglayout="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

importmatplotlib.pyplotaspltimportmatplotlib.axesimportmatplotlib.gridspecimportmatplotlib._layoutgriddefmake_demo(plot_name,outer_space=0.1,inner_space=0.1,layout=None,):file_name="%s.png"%plot_nameprint(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()assertisinstance(a0,matplotlib.axes.Axes)assertisinstance(a1,matplotlib.axes.Axes)foraxisin [a0,a1]:axis.get_xaxis().set_visible(False)axis.get_yaxis().set_visible(False)forsinaxis.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)deffix_layout_grid():defaults=matplotlib._layoutgrid.LayoutGrid.__init__.__defaults__defaults=list(defaults)defaults[2]=Truedefaults=tuple(defaults)matplotlib._layoutgrid.LayoutGrid.__init__.__defaults__=defaultsif__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 usinglayout="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 thematplotlib installation (included in thefix_layout_grid function above):

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

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

  1. Inhttps://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/_constrained_layout.py#L226 , inlayoutgrids[rep] = mlayoutgrid.LayoutGrid(...), includeparent_inner=True
  2. Inhttps://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/_layoutgrid.py#L36 , inLayoutGrid.__init__(...), changeparent_inner=False toparent_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


      [8]ページ先頭

      ©2009-2025 Movatter.jp