Skip to content

Commit 31e9bfe

Browse files
authored
Merge pull request #10950 from dstansby/log-invalid-values
Actually ignore invalid log-axis limit setting
2 parents 3ec18e3 + a5b071b commit 31e9bfe

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

lib/matplotlib/axes/_base.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -3103,10 +3103,20 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
31033103
'left=%s, right=%s') % (left, right))
31043104
left, right = mtransforms.nonsingular(left, right, increasing=False)
31053105

3106-
if self.get_xscale() == 'log' and (left <= 0.0 or right <= 0.0):
3107-
warnings.warn(
3108-
'Attempted to set non-positive xlimits for log-scale axis; '
3109-
'invalid limits will be ignored.')
3106+
if self.get_xscale() == 'log':
3107+
if left <= 0:
3108+
warnings.warn(
3109+
'Attempted to set non-positive left xlim on a '
3110+
'log-scaled axis.\n'
3111+
'Invalid limit will be ignored.')
3112+
left = old_left
3113+
if right <= 0:
3114+
warnings.warn(
3115+
'Attempted to set non-positive right xlim on a '
3116+
'log-scaled axis.\n'
3117+
'Invalid limit will be ignored.')
3118+
right = old_right
3119+
31103120
left, right = self.xaxis.limit_range_for_scale(left, right)
31113121

31123122
self.viewLim.intervalx = (left, right)
@@ -3423,10 +3433,19 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
34233433

34243434
bottom, top = mtransforms.nonsingular(bottom, top, increasing=False)
34253435

3426-
if self.get_yscale() == 'log' and (bottom <= 0.0 or top <= 0.0):
3427-
warnings.warn(
3428-
'Attempted to set non-positive ylimits for log-scale axis; '
3429-
'invalid limits will be ignored.')
3436+
if self.get_yscale() == 'log':
3437+
if bottom <= 0:
3438+
warnings.warn(
3439+
'Attempted to set non-positive bottom ylim on a '
3440+
'log-scaled axis.\n'
3441+
'Invalid limit will be ignored.')
3442+
bottom = old_bottom
3443+
if top <= 0:
3444+
warnings.warn(
3445+
'Attempted to set non-positive top ylim on a '
3446+
'log-scaled axis.\n'
3447+
'Invalid limit will be ignored.')
3448+
top = old_top
34303449
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
34313450

34323451
self.viewLim.intervaly = (bottom, top)

lib/matplotlib/tests/test_scale.py

+24
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,27 @@ def test_logscale_nonpos_values():
121121

122122
ax4.set_yscale('log')
123123
ax4.set_xscale('log')
124+
125+
126+
def test_invalid_log_lims():
127+
# Check that invalid log scale limits are ignored
128+
fig, ax = plt.subplots()
129+
ax.scatter(range(0, 4), range(0, 4))
130+
131+
ax.set_xscale('log')
132+
original_xlim = ax.get_xlim()
133+
with pytest.warns(UserWarning):
134+
ax.set_xlim(left=0)
135+
assert ax.get_xlim() == original_xlim
136+
with pytest.warns(UserWarning):
137+
ax.set_xlim(right=-1)
138+
assert ax.get_xlim() == original_xlim
139+
140+
ax.set_yscale('log')
141+
original_ylim = ax.get_ylim()
142+
with pytest.warns(UserWarning):
143+
ax.set_ylim(bottom=0)
144+
assert ax.get_ylim() == original_ylim
145+
with pytest.warns(UserWarning):
146+
ax.set_ylim(top=-1)
147+
assert ax.get_ylim() == original_ylim

0 commit comments

Comments
 (0)