Skip to content

Commit 1c726d5

Browse files
authored
Merge pull request #7744 from bcongdon/invalid-xlim-error
FIX: Added axis limit check for non-finite values
2 parents 3078330 + df04ff9 commit 1c726d5

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Invalid (Non-finite) Axis Limit Error
2+
-------------------------------------
3+
4+
When using :func:`set_xlim` and :func:`set_ylim`, passing non-finite values now
5+
results in a ValueError. The previous behavior resulted in the limits being
6+
erroneously reset to `(-0.001, 0.001)`.

lib/matplotlib/axes/_base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
28782878
if right is not None:
28792879
right = self.convert_xunits(right)
28802880

2881+
if ((left is not None and not np.isfinite(left)) or
2882+
(right is not None and not np.isfinite(right))):
2883+
raise ValueError("Specified x limits must be finite; "
2884+
"instead, found: (%s, %s)" % (left, right))
2885+
28812886
old_left, old_right = self.get_xlim()
28822887
if left is None:
28832888
left = old_left
@@ -3172,6 +3177,11 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
31723177
if top is not None:
31733178
top = self.convert_yunits(top)
31743179

3180+
if ((top is not None and not np.isfinite(top)) or
3181+
(bottom is not None and not np.isfinite(bottom))):
3182+
raise ValueError("Specified y limits must be finite; "
3183+
"instead, found: (%s, %s)" % (bottom, top))
3184+
31753185
old_bottom, old_top = self.get_ylim()
31763186

31773187
if bottom is None:

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4972,3 +4972,15 @@ def test_bar_single_height():
49724972
ax.bar(range(4), 1)
49734973
# Check that a horizontal chart with one width works
49744974
ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal')
4975+
4976+
4977+
def test_invalid_axis_limits():
4978+
plt.plot([0, 1], [0, 1])
4979+
with pytest.raises(ValueError):
4980+
plt.xlim(np.nan)
4981+
with pytest.raises(ValueError):
4982+
plt.xlim(np.inf)
4983+
with pytest.raises(ValueError):
4984+
plt.ylim(np.nan)
4985+
with pytest.raises(ValueError):
4986+
plt.ylim(np.inf)

0 commit comments

Comments
 (0)