You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Nondeterministic behavior with subplot spacing and constrained layout #28574
I'm trying to get constrained layout to remove all space between subplots when no artists should be preventing it. In this example I've set all necessary rc params accordingly and changed the axes limits to ensure the x-axis tick labels don't spill beyond the axis extent. The resulting space between the axes is nondeterministic.
Example output (ran a handful of times until one of each result showed):
Expected outcome
Zero spacing between the axes, deterministically.
Additional information
I noticed that the results became deterministic (with zero space between panels) when saving the figure (that is, the result that displayed in the notebook, not just the saved figure). Inserting a fig.canvas.draw() at the end likewise ensures zero space deterministically.
I think this is expected behaviour and I'm not sure if we really can do anything about it. This happens because when the constraints are first computed the xtick on the right hand side of the axis horizontally overspills the axis bounding box, so constrained layout adds a bit of space. The axes gets redrawn, the axis is larger, and now the tick label fits.
I don't think there is anything to directly be done about this. I think you could take the tick label out of the layout, but that is a bit fiddily. If you want the axes to always exactly touch, then I'm not sure I'd bother using constrained layout.
This happens because when the constraints are first computed the xtick on the right hand side of the axis horizontally overspills the axis bounding box, so constrained layout adds a bit of space. The axes gets redrawn, the axis is larger, and now the tick label fits.
I'm not sure I follow why this should be nondeterministic between identical executions. I do think the zero-space result is the correct one (following from what I stated under "additional information"), but even if it isn't I don't see why the result should be nondeterministic.
I think you could take the tick label out of the layout, but that is a bit fiddily.
As an aside, I would like to know how to do this (using set_in_layout on the tick labels' Text instances didn't work) - but I asked about it on discourse because it's a separate question that I'm not yet sure is a bug.
This is a complicated interplay of our tick system being too customizable, being too lazy, and IMO, some weird cross-ownership of the calculation of tight bounding boxes. In order to calculate the bounding box of the Axes, the Spine is the one that handles the tick lines (which is odd to me, since the Axis owns the Ticks):
In doing so, it picks a major tick, and a minor tick, and adds padding based on the direction in the tick's _tickdir attribute. However, it does this by passing the tick lists through some sets, meaning it will pick a random one depending on the set.
Ticks are generated lazily, and copy properties from the initial tick:
"""Copy the properties from *src* tick to *dest* tick."""
ifsrcisNoneordestisNone:
return
dest.label1.update_from(src.label1)
dest.label2.update_from(src.label2)
dest.tick1line.update_from(src.tick1line)
dest.tick2line.update_from(src.tick2line)
dest.gridline.update_from(src.gridline)
This copies the properties from sub-Artist to sub-Artist, but not the _tickdir attribute (or any others for that matter). So it will be whatever the default rcParam is at the time of the copy. If you run a draw in the context, the copies happen inside, and are all _tickdir='in'. If you don't, then some ticks will have the right value, and some won't.
This means that the Spine will sometimes calculate a bounding box for a inward tick, and sometimes one for an outward tick, depending on which random tick it used.
Indeed, setting the rcParams in my example globally rather than as a context appears to ensure determinism. Also helps explain the inconsistent behavior I was observing more generally (since I was using rc contexts to facilitate testing, whereas I normally set a style globally).
Many thanks for digging into it (and addressing it so quickly)!
@QuLogic, would your observations explain why calling set_in_layout(False) on all tick labels has no effect on the constrained layout result?
I'm actually struggling to get set_in_layout to have any effect on any artist at all. Even ham-fistedly applying it to all axes' children has no effect, e.g.,
Let me know whether to open a new issue. I'm really struggling to study the issue in more detail because, e.g., I can't figure out how to properly draw bboxes on figures. Copying this color_boxes method and using it as in those tests draws boxes that only cover ~the lower-left quadrant of the axes, e.g.,
The bad bboxes are because you are drawing in pixel co-ordinates, but Matplotlib doubles the pixels for retina displays. I wouldn't dig code out of the tests and expect it to work on arbitrary code. In particular transform=None is not going to work for that situation.
As for taking everything out of layout - that may not work as not everything checks the in_layout setting when calculating the tightbboxes. One would have to track all the elements down and then decide if it makes sense to allow them to be excluded. It is possible that the axes elements have never been excluded.
The bad bboxes are because you are drawing in pixel co-ordinates, but Matplotlib doubles the pixels for retina displays.
Argh, thanks, I set that in my jupyter configuration so many years ago I completely forgot about it.
I wouldn't dig code out of the tests and expect it to work on arbitrary code. In particular transform=None is not going to work for that situation.
Do the tests set some global configuration that's not evident in the files themselves, or is just my own config to worry about? I scoured the repo, issues, discourse, and google for other up-to-date solutions before realizing the tests should be as authoritative as it gets.
As for taking everything out of layout - that may not work as not everything checks the in_layout setting when calculating the tightbboxes. One would have to track all the elements down and then decide if it makes sense to allow them to be excluded. It is possible that the axes elements have never been excluded.
Activity
jklymak commentedon Jul 15, 2024
I think this is expected behaviour and I'm not sure if we really can do anything about it. This happens because when the constraints are first computed the xtick on the right hand side of the axis horizontally overspills the axis bounding box, so constrained layout adds a bit of space. The axes gets redrawn, the axis is larger, and now the tick label fits.
I don't think there is anything to directly be done about this. I think you could take the tick label out of the layout, but that is a bit fiddily. If you want the axes to always exactly touch, then I'm not sure I'd bother using constrained layout.
jklymak commentedon Jul 15, 2024
Btw it maybe could be more clear, but this is the case covered by caveat 2 here https://matplotlib.org/stable/users/explain/axes/constrainedlayout_guide.html#other-caveats
zachjweiner commentedon Jul 15, 2024
I'm not sure I follow why this should be nondeterministic between identical executions. I do think the zero-space result is the correct one (following from what I stated under "additional information"), but even if it isn't I don't see why the result should be nondeterministic.
As an aside, I would like to know how to do this (using
set_in_layouton the tick labels'Textinstances didn't work) - but I asked about it on discourse because it's a separate question that I'm not yet sure is a bug.QuLogic commentedon Jul 15, 2024
This is a complicated interplay of our tick system being too customizable, being too lazy, and IMO, some weird cross-ownership of the calculation of tight bounding boxes. In order to calculate the bounding box of the
Axes, theSpineis the one that handles the tick lines (which is odd to me, since theAxisowns theTicks):matplotlib/lib/matplotlib/spines.py
Lines 160 to 178 in 9b8a8c7
In doing so, it picks a major tick, and a minor tick, and adds padding based on the direction in the tick's
_tickdirattribute. However, it does this by passing the tick lists through some sets, meaning it will pick a random one depending on the set.Ticks are generated lazily, and copy properties from the initial tick:
matplotlib/lib/matplotlib/axis.py
Lines 1611 to 1619 in 9b8a8c7
This copies the properties from sub-Artist to sub-Artist, but not the
_tickdirattribute (or any others for that matter). So it will be whatever the default rcParam is at the time of the copy. If you run a draw in the context, the copies happen inside, and are all_tickdir='in'. If you don't, then some ticks will have the right value, and some won't.This means that the
Spinewill sometimes calculate a bounding box for a inward tick, and sometimes one for an outward tick, depending on which random tick it used.Copy all internals from initial Tick to lazy ones
zachjweiner commentedon Jul 15, 2024
Indeed, setting the rcParams in my example globally rather than as a context appears to ensure determinism. Also helps explain the inconsistent behavior I was observing more generally (since I was using rc contexts to facilitate testing, whereas I normally set a style globally).
Many thanks for digging into it (and addressing it so quickly)!
zachjweiner commentedon Jul 15, 2024
@QuLogic, would your observations explain why calling
set_in_layout(False)on all tick labels has no effect on the constrained layout result?I'm actually struggling to get
set_in_layoutto have any effect on any artist at all. Even ham-fistedly applying it to all axes' children has no effect, e.g.,Let me know whether to open a new issue. I'm really struggling to study the issue in more detail because, e.g., I can't figure out how to properly draw bboxes on figures. Copying this
color_boxesmethod and using it as in those tests draws boxes that only cover ~the lower-left quadrant of the axes, e.g.,yields

jklymak commentedon Jul 15, 2024
The bad bboxes are because you are drawing in pixel co-ordinates, but Matplotlib doubles the pixels for retina displays. I wouldn't dig code out of the tests and expect it to work on arbitrary code. In particular
transform=Noneis not going to work for that situation.As for taking everything out of layout - that may not work as not everything checks the in_layout setting when calculating the tightbboxes. One would have to track all the elements down and then decide if it makes sense to allow them to be excluded. It is possible that the axes elements have never been excluded.
zachjweiner commentedon Jul 15, 2024
Argh, thanks, I set that in my jupyter configuration so many years ago I completely forgot about it.
Do the tests set some global configuration that's not evident in the files themselves, or is just my own config to worry about? I scoured the repo, issues, discourse, and google for other up-to-date solutions before realizing the tests should be as authoritative as it gets.
Does the docstring for get_tightbbox (and this line in
get_default_bbox_extra_artists) not suggest that they are meant to be excluded?jklymak commentedon Jul 15, 2024
Someone would have to check the code paths for getting bounding boxes, which are somewhat convoluted.
Copy all internals from initial Tick to lazy ones
Copy all internals from initial Tick to lazy ones