Fix Axes3D.get_tightbbox to include axis labels when not for_layout_only#31571
Open
Scolliq wants to merge 1 commit intomatplotlib:mainfrom
Open
Fix Axes3D.get_tightbbox to include axis labels when not for_layout_only#31571Scolliq wants to merge 1 commit intomatplotlib:mainfrom
Scolliq wants to merge 1 commit intomatplotlib:mainfrom
Conversation
When savefig(bbox_inches='tight') is used on a figure with 3D axes, axis labels (zlabel, xlabel, ylabel) were being clipped because Axes3D.get_tightbbox unconditionally called _get_tightbbox_for_layout_only for each axis, which always passes for_layout_only=True and therefore excludes labels from the bbox. Fix: when the outer call has for_layout_only=False (the savefig path), call axis.get_tightbbox(renderer, for_layout_only=False) directly so that labels are included in the bounding box calculation. The for_layout_only=True path (layout engine) continues to use the existing helper to avoid affecting constrained/tight layout behaviour. Fixes matplotlib#28117
Member
|
Hello @Scolliq, I see you opened two PRs at the same time. We ask that new contributors not open a second PR until the first is closed Please could you decide which of your PRs you would like to move forward with and close the other for now. |
Member
|
Since your PRs look AI driven, please also familiarise yourself with our policy on that |
Author
|
Apologies did not know about the one per person for the initial. The other has been closed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When saving a 3D figure with
fig.savefig("out.png", bbox_inches='tight'),axis labels (
zlabel,xlabel,ylabel) are clipped becauseAxes3D.get_tightbboxunconditionally calls_get_tightbbox_for_layout_onlyfor each 3D axis, which always passesfor_layout_only=Trueand therefore explicitly excludes labels fromthe bounding box.
Fixes #28117.
Fix
In
Axes3D.get_tightbbox, differentiate between the two call paths:for_layout_only=True(layout engine): continue using_get_tightbbox_for_layout_onlyso layout calculations are unaffectedfor_layout_only=False(savefig tight path): callaxis.get_tightbbox(renderer, for_layout_only=False)directly so thatlabels are included in the bounding box
Tests
Added
test_axes3d_tightbbox_includes_axis_labelswhich:(labels expand the bbox)
savefig(bbox_inches='tight')completes without error