Skip to content

Conversation

@saumyacoder1709
Copy link

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

Can anyone tell me what is the meaning of failed tests. Is there any correction that I can make in PR? Kindly help me.

@timhoffm
Copy link
Member

timhoffm commented Dec 13, 2025

The test failure is unrelated. See #30703 (comment).

Note: It‘s unnecessary and discouraged to close a PR and open a new one for the same topic, as you‘ve done here/in #30826. For the future, please force-push instead if needed.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Please add a test.

if other is self.axes:
continue
other._axis_map[name]._set_lim(v0, v1, emit=False, auto=auto)
# Skip propagating default (0, 1) limits from linear scale to
Copy link
Member

Choose a reason for hiding this comment

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

Please convince me that this is the correct solution and not just fixing a special case. I don’t have full oversight on the details of the topic, but doing selective action on scale and limits doesn’t feel like it it’s a general solution.

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