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

Skip to content

Commit 94ac78a

Browse files
committed
Fix bug that caused a crash when setting negative limits and using log scale
1 parent 896fb81 commit 94ac78a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 1 deletion
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 '

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,14 @@ 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+
2151+
21442152
@image_comparison(['stackplot_test_image', 'stackplot_test_image'])
21452153
def test_stackplot():
21462154
fig = plt.figure()

0 commit comments

Comments
 (0)