Skip to content

Commit 22387cc

Browse files
committed
Added set_xlim and set_ylim check for non-finite limit values
1 parent 015d8d5 commit 22387cc

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-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
@@ -2875,6 +2875,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
28752875
if right is not None:
28762876
right = self.convert_xunits(right)
28772877

2878+
if ((left is not None and not np.isfinite(left)) or
2879+
(right is not None and not np.isfinite(right))):
2880+
raise ValueError("xlim limits must be finite. "
2881+
"instead, found: (%s, %s)" % (left, right))
2882+
28782883
old_left, old_right = self.get_xlim()
28792884
if left is None:
28802885
left = old_left
@@ -3154,6 +3159,11 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
31543159
if top is not None:
31553160
top = self.convert_yunits(top)
31563161

3162+
if ((top is not None and not np.isfinite(top)) or
3163+
(bottom is not None and not np.isfinite(bottom))):
3164+
raise ValueError("ylim limits must be finite. "
3165+
"instead, found: (%s, %s)" % (top, bottom))
3166+
31573167
old_bottom, old_top = self.get_ylim()
31583168

31593169
if bottom is None:

lib/matplotlib/tests/test_axes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,3 +4877,16 @@ def test_scatter_color_masking():
48774877
def test_eventplot_legend():
48784878
plt.eventplot([1.0], label='Label')
48794879
plt.legend()
4880+
4881+
4882+
@cleanup
4883+
def test_invalid_axis_limits():
4884+
plt.plot([0, 1], [0, 1])
4885+
with pytest.raises(ValueError):
4886+
plt.xlim(np.nan)
4887+
with pytest.raises(ValueError):
4888+
plt.xlim(np.inf)
4889+
with pytest.raises(ValueError):
4890+
plt.ylim(np.nan)
4891+
with pytest.raises(ValueError):
4892+
plt.ylim(np.inf)

0 commit comments

Comments
 (0)