Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32a111b

Browse files
authored
Merge pull request #14635 from astrofrog/fix-negative-log-limits
Fix bug when setting negative limits and using log scale
2 parents 4a56315 + e71f881 commit 32a111b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,7 +3249,10 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
32493249
if right is None:
32503250
right = old_right
32513251

3252-
if self.get_xscale() == 'log':
3252+
if self.get_xscale() == 'log' and (left <= 0 or right <= 0):
3253+
# Axes init calls set_xlim(0, 1) before get_xlim() can be called,
3254+
# so only grab the limits if we really need them.
3255+
old_left, old_right = self.get_xlim()
32533256
if left <= 0:
32543257
cbook._warn_external(
32553258
'Attempted to set non-positive left xlim on a '
@@ -3629,7 +3632,10 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
36293632
if top is None:
36303633
top = old_top
36313634

3632-
if self.get_yscale() == 'log':
3635+
if self.get_yscale() == 'log' and (bottom <= 0 or top <= 0):
3636+
# Axes init calls set_xlim(0, 1) before get_xlim() can be called,
3637+
# so only grab the limits if we really need them.
3638+
old_bottom, old_top = self.get_ylim()
36333639
if bottom <= 0:
36343640
cbook._warn_external(
36353641
'Attempted to set non-positive bottom ylim on a '

lib/matplotlib/tests/test_axes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,17 @@ def test_log_scales():
21412141
ax.set_xscale('log', basex=9.0)
21422142

21432143

2144+
def test_log_scales_invalid():
2145+
fig = plt.figure()
2146+
ax = fig.add_subplot(1, 1, 1)
2147+
ax.set_xscale('log')
2148+
with pytest.warns(UserWarning, match='Attempted to set non-positive'):
2149+
ax.set_xlim(-1, 10)
2150+
ax.set_yscale('log')
2151+
with pytest.warns(UserWarning, match='Attempted to set non-positive'):
2152+
ax.set_ylim(-1, 10)
2153+
2154+
21442155
@image_comparison(['stackplot_test_image', 'stackplot_test_image'])
21452156
def test_stackplot():
21462157
fig = plt.figure()

0 commit comments

Comments
 (0)