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

Skip to content

Commit d633b8a

Browse files
committed
Added set_xlim and set_ylim check for non-finite limit values
1 parent 924d913 commit d633b8a

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

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
@@ -3169,6 +3174,11 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
31693174
if top is not None:
31703175
top = self.convert_yunits(top)
31713176

3177+
if ((top is not None and not np.isfinite(top)) or
3178+
(bottom is not None and not np.isfinite(bottom))):
3179+
raise ValueError("ylim limits must be finite. "
3180+
"instead, found: (%s, %s)" % (top, bottom))
3181+
31723182
old_bottom, old_top = self.get_ylim()
31733183

31743184
if bottom is None:

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4931,3 +4931,15 @@ def test_bar_single_height():
49314931
ax.bar(range(4), 1)
49324932
# Check that a horizontal chart with one width works
49334933
ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal')
4934+
4935+
4936+
def test_invalid_axis_limits():
4937+
plt.plot([0, 1], [0, 1])
4938+
with pytest.raises(ValueError):
4939+
plt.xlim(np.nan)
4940+
with pytest.raises(ValueError):
4941+
plt.xlim(np.inf)
4942+
with pytest.raises(ValueError):
4943+
plt.ylim(np.nan)
4944+
with pytest.raises(ValueError):
4945+
plt.ylim(np.inf)

0 commit comments

Comments
 (0)