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

Skip to content

Commit 3fc50c7

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

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3240,10 +3240,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
32403240
left = self._validate_converted_limits(left, self.convert_xunits)
32413241
right = self._validate_converted_limits(right, self.convert_xunits)
32423242

3243+
old_left, old_right = self.get_xlim()
3244+
32433245
if left is None or right is None:
32443246
# Axes init calls set_xlim(0, 1) before get_xlim() can be called,
32453247
# so only grab the limits if we really need them.
3246-
old_left, old_right = self.get_xlim()
32473248
if left is None:
32483249
left = old_left
32493250
if right is None:

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)