FIX: Prevent warning when clearing axes with shared non-linear scale#30826
FIX: Prevent warning when clearing axes with shared non-linear scale#30826saumyacoder1709 wants to merge 1 commit intomatplotlib:mainfrom
Conversation
2321eab to
b5abeb2
Compare
When clearing an axes (via cla() or clf()) that has a shared axis with a non-linear scale (e.g., log, logit), a warning was incorrectly generated: 'Attempt to set non-positive xlim on a log-scaled axis will be ignored.' This occurred because when an axes with linear scale sets default limits (0, 1), these limits propagate to shared axes that may have non-linear scales which reject these limits. Fixed by skipping propagation of default (0, 1) limits from linear scale axes to non-linear scale shared axes. This preserves the behavior for other cases (like inverted axes) while eliminating the spurious warning. Additionally, reordered scale assignment before limit setting in sharex()/sharey() methods to ensure scale is set before limits are applied. Fixes matplotlib#9970
b5abeb2 to
2e53341
Compare
|
I have made a PR. If there are any improvements, suggest me and I will incorporate that too. |
I don't understand this explanation. I cannot find anywhere in this change where |
|
I have updated the PR. The earlier PR was not updated to my newer approach. Apologies for that @QuLogic. Kindly let me know if there is any other requirement or if some changes are needed in my PR. |
PR summary
When clearing an axes (via cla() or clf()) that has a shared axis with a non-linear scale (e.g., log, logit), an incorrect warning was generated:
Attempt to set non-positive xlim on a log-scaled axis will be ignored.
Why is this change necessary?
This warning appeared even though the user's code was perfectly valid. Clearing axes with shared non-linear scales is a common operation and should not generate spurious warnings.
What problem does it solve?
The issue occurred during the axis limit propagation in the Axis._set_lim() method in axis.py. When an axes with linear scale clears and resets to default limits (0, 1), these limits automatically propagate to all shared axes. If a shared axis has a non-linear scale (like log), it rejects these limits because they contain non-positive values, triggering the warning.
What is the reasoning for this implementation?
The fix adds a targeted check in the limit propagation loop (axis.py, lines 1267-1272) to skip propagating default (0, 1) limits when:
The source axis has linear scale, AND
The receiving shared axis has a non-linear scale, AND
The limits being propagated are exactly (0, 1)
This prevents the spurious warning while preserving all other propagation behavior (including for inverted shared axes, which rely on propagation to work correctly).
Additionally, in _base.py (lines 1278 and 1298), the sharex() and sharey() methods now set the scale before copying limits from the shared axis. This prevents temporary scale/limit mismatches during axis sharing initialization.
Minimal self-contained example:
PR checklist