Skip to content

Fix restoring 'auto' aspect in 3D axes after switching from 'equal'#31304

Open
jayaprajapatii wants to merge 2 commits intomatplotlib:mainfrom
jayaprajapatii:fix-3dplot-aspect-back-to-auto
Open

Fix restoring 'auto' aspect in 3D axes after switching from 'equal'#31304
jayaprajapatii wants to merge 2 commits intomatplotlib:mainfrom
jayaprajapatii:fix-3dplot-aspect-back-to-auto

Conversation

@jayaprajapatii
Copy link
Contributor

Closes #31276

Switching the aspect of a 3D axis from "auto" to "equal" and then back to "auto" did not restore the original plot correctly. The logic for handling "auto" continued executing the equal-aspect adjustments, which modified the axis limits and box aspect.

This change ensures that when aspect == "auto", the box aspect is reset and the function exits early, preventing further equal-aspect logic from running.

The baseline image for test_aspects has been updated to reflect the corrected rendering.

@rcomer
Copy link
Member

rcomer commented Mar 14, 2026

I'm not too familiar with 3D plotting, but the test explicitly set the box aspect. If a user did similarly, shouldn't we respect it?

ax.set_box_aspect((3, 4, 5))
ax.set_aspect(aspects[i], adjustable='datalim')

@jayaprajapatii
Copy link
Contributor Author

Thanks @rcomer
My intention with the early return was to avoid the equal-aspect adjustment logic from running when aspect="auto", since it was continuing to modify the axis limits and box aspect.
You're right that the test explicitly sets a box aspect. In that case we should respect a user-defined set_box_aspect.

Would it make more sense to only reset the box aspect when it was implicitly modified by the equal-aspect logic, rather than when it was explicitly set by the user?

@QuLogic QuLogic requested a review from scottshambaugh March 17, 2026 21:33
@scottshambaugh
Copy link
Contributor

I think that overriding a previously set aspect with "auto" is the correct behavior. But we should have different behavior with setting "auto" on the "datalim" - the box shouldn't be modified in that case.

Copy link
Contributor

@scottshambaugh scottshambaugh left a comment

Choose a reason for hiding this comment

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

The test image was changed in separate commits, so that needs to be git squashed or rebased to fix the "PR Cleanliness" failure.

Also, this new behavior needs to be tested.

@jayaprajapatii jayaprajapatii force-pushed the fix-3dplot-aspect-back-to-auto branch from e8db81d to a4d63fa Compare March 22, 2026 17:10
Copy link
Contributor

@scottshambaugh scottshambaugh left a comment

Choose a reason for hiding this comment

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

Ok, I think this is looking good for the adjustable='box' case. What's the current behavior for 'datalim'? That should be fixed at the same time as well.

initial_limits = ax.get_w_lims()

ax.set_aspect("equal")
fig.canvas.draw()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add assert not np.allclose(initial_limits, new_limits) here to ensure the limits do actually change.

@jayaprajapatii
Copy link
Contributor Author

Hello @scottshambaugh

Updated set_aspect('auto') to respect adjustable. For adjustable='box', the box aspect is reset as expected.
For adjustable='datalim', the existing behavior is preserved.

I verified that in the datalim case, switching to 'equal' does not lead to a meaningful change in limits, so I did not add an assertion for limit differences.
Added a test to cover the adjustable='datalim' behavior.

@scottshambaugh
Copy link
Contributor

What is the current behavior for 'datalim'? Please share screenshots, and try to set up a case where this is exercised. I don't think the test added for the datalim case is meaningfully doing anything right now.

@jayaprajapatii
Copy link
Contributor Author

Hello,
I rechecked the datalim case with a minimal example.

Observed behavior:
Initial limits, after setting 'equal', and after switching back to 'auto' all remain the same. So in the 'datalim' case, aspect changes do not seem to affect limits.

Screenshot 2026-03-25 032213 Screenshot 2026-03-25 032247

Given this behavior, I’m trying to determine what the intended assertion should be for the datalim case. Could you clarify what aspect of the behavior we should validate here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Setting aspect back to auto cannot recover the original 3D plot

3 participants