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]: Figure.align_labels() confused by GridSpecFromSubplotSpec #29248

Closed
Milestone
@neutrinonerd3333

Description

@neutrinonerd3333

Bug summary

In a composite figure with nested gridspecs,Figure.align_labels() (andalign_xlabels(),align_ylabels()) can end up aligning labels that should not intuitively be. Likewise withalign_titles().

Code for reproduction

fig=plt.figure(figsize=(6,4))gs0=gridspec.GridSpec(nrows=1,ncols=2,figure=fig)gs00=gs0[0].subgridspec(nrows=2,ncols=1,height_ratios=[8,8])gs01=gs0[1].subgridspec(nrows=2,ncols=1,height_ratios=[9,6])left_axs=gs00.subplots()right_axs=gs01.subplots()left_axs[0].set_ylim(0,0.02)# to force nontrivial alignmentleft_axs[0].set_ylabel('foo')left_axs[1].set_ylabel('bar')right_axs[0].set_ylabel('baz')right_axs[1].set_ylabel('qux')left_axs[1].set_title('title')right_axs[1].set_title('title')fig.align_labels()fig.align_titles()

Actual outcome

All labels are aligned. Titles are aligned as well.

image

Expected outcome

Labels in separate columns are aligned, but labels in different columns should not be. Titles are not aligned:

image

Additional information

Right now, the ylabel (xlabel) alignment code seems to attempt to align labels on Axes with the same column index (resp. row index) without checking if those indexes are for the same gridspec. To fix this, we should probably add a check that two Axes share the same gridspec (in addition to being in the same row/col) before we align their labels. (This would not allow label alignment across gridspecs, but if a user wants to align labels between two Axes, it seems reasonable to expect them to put the Axes in the same gridspec.)

The same thing happens with align_titles().

For now, a workaround for labels is to callFigure.align_labels() separately for each sub-gridspec with theaxs kwarg (as done for the expected outcome figure above).

Operating system

macOS 14.1.1

Matplotlib Version

3.9.2

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.12.2

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp