Skip to content

Conversation

@saumyacoder1709
Copy link

@saumyacoder1709 saumyacoder1709 commented Dec 8, 2025

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:

import matplotlib.pyplot as plt

# Before fix: generates warning
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.set_xscale('log')
fig.clf()  # Warning: "Attempt to set non-positive xlim..."

# After fix: no warning

PR checklist

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
@saumyacoder1709
Copy link
Author

saumyacoder1709 commented Dec 8, 2025

I have made a PR. If there are any improvements, suggest me and I will incorporate that too.

@QuLogic
Copy link
Member

QuLogic commented Dec 9, 2025

What problem does it solve? The issue occurred in the __clear() method in lib/matplotlib/axes/_base.py. When clearing an axes, default limits (0, 1) were set with emit=True (the default), causing these limits to propagate to shared axes that still had non-linear scales. Since (0, 1) contains non-positive values on the boundary, log scales reject them and generate a warning.

What is the reasoning for this implementation? The fix uses emit=False when setting default limits during the clear operation. This prevents the default limits from propagating to shared axes, which will receive their correct limits from the sharing mechanism instead. This is a minimal, targeted fix that preserves the existing behavior while eliminating the spurious warning.

I don't understand this explanation. I cannot find anywhere in this change where emit changed from True to False.

@saumyacoder1709
Copy link
Author

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.

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.

clearing axes with shared axis with non-linear scales should not warn

2 participants