Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix removal of colorbars on nested subgridspecs. #27360

Merged
merged 1 commit into from
Nov 23, 2023

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Nov 22, 2023

Indiscriminately going back up to the topmost gridspec is wrong in the presence of nested gridspecs. Closes #27329.

We may want to add an API to just go up one level in gridspecs (i.e. public access to _subplot_spec) but I'd like to first consider whether we could e.g. merge GridSpecFromSubplotSpec into GridSpec with the only difference that the former's get_parent() (or another similarly named new method) returns another gridspec whereas the latter's returns a figure.

PR summary

PR checklist

Indiscriminately going back up to the topmost gridspec is wrong in the
presence of nested gridspecs.

We may want to add an API to just go up one level in gridspecs (i.e.
public access to _subplot_spec) but I'd like to first consider whether
we could e.g. merge GridSpecFromSubplotSpec into GridSpec with the only
difference that the former's get_parent() (or another similarly named
new method) returns another gridspec whereas the latter's returns a
figure.
@anntzer anntzer added topic: geometry manager LayoutEngine, Constrained layout, Tight layout topic: color/colorbar labels Nov 22, 2023
subplotspec = gs.get_topmost_subplotspec()
except AttributeError:
# use_gridspec was False
subplotspec = self.ax.get_subplotspec().get_gridspec()._subplot_spec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you walk through how this works? Where did _subplot_spec come from and why is it defined on the parent gridspec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The classic (tighr_layout, pre-CL) way of positioning colorbars (assuming that the parent axes is positioned with a subplotspec) is to create a subgridspec on the parent subplotspec. For example say we had initially

A B

C D

and we choose to add a colorbar to axes D (which was previously positioned at gs = GridSpec(2, 2)[1, 1]). We create a subgridspec sgs = GridSpec(2, 2)[1, 1].subgridspec(3, 2) so that the block where we previously had D is replace by (zoomed in)

D .
D c
D .

where D is now at sgs[:, 1] and c (the colorbar) at sgs[1, 1] (and likewise for other colorbar locations).
Now if we remove the colorbar, we want to move back D to gs, which is exactly equal to sgs._subplot_spec (*); sgs itself is equal to c.get_subplotspec().get_gridspec().

The code prior to this PR did not go up one nesting layer, but rather climbed all of the nesting layers indiscriminately (get_topmost_subplotspec), which would be problematic if the whole thing way already layered into other nested subgridspecs.

(*) as noted in the message, maybe a public API for _subplot_spec would be nice, but that's another issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see - _subplot_spec is the subplot spec of the (Sub)GridSpec. Makes sense.

@QuLogic QuLogic added this to the v3.8.3 milestone Nov 23, 2023
@QuLogic QuLogic merged commit 412a5a0 into matplotlib:main Nov 23, 2023
40 of 43 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Nov 23, 2023
@QuLogic QuLogic modified the milestones: v3.8.3, v3.9.0 Nov 24, 2023
@anntzer anntzer deleted the cbngs branch December 11, 2023 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: color/colorbar topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Projects
None yet
3 participants