Skip to content

Commit 5cbedf5

Browse files
author
Vedant Nanda
committed
error raised for invalid axes limits
1 parent c907ebb commit 5cbedf5

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,11 +2874,13 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
28742874

28752875
self._process_unit_info(xdata=(left, right))
28762876
if left is not None:
2877-
if isinstance(left,float) and (math.isnan(left) or np.isinf(left)):
2877+
if (isinstance(left, float) and
2878+
(not np.isreal(left) or not np.isfinite(left))):
28782879
raise ValueError("NaN or Inf cannot be the argument values")
28792880
left = self.convert_xunits(left)
28802881
if right is not None:
2881-
if isinstance(right,float) and (math.isnan(right) or np.isinf(right)):
2882+
if (isinstance(right, float) and
2883+
(not np.isreal(right) or not np.isfinite(right))):
28822884
raise ValueError("NaN or Inf cannot be the argument values")
28832885
right = self.convert_xunits(right)
28842886

@@ -3172,8 +3174,14 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
31723174
bottom, top = bottom
31733175

31743176
if bottom is not None:
3177+
if (isinstance(bottom, float) and
3178+
(not np.isreal(bottom) or not np.isfinite(bottom))):
3179+
raise ValueError("NaN or Inf cannot be the argument values")
31753180
bottom = self.convert_yunits(bottom)
31763181
if top is not None:
3182+
if (isinstance(top, float) and
3183+
(not np.isreal(top) or not np.isfinite(top))):
3184+
raise ValueError("NaN or Inf cannot be the argument values")
31773185
top = self.convert_yunits(top)
31783186

31793187
old_bottom, old_top = self.get_ylim()

lib/matplotlib/tests/test_axes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4972,3 +4972,25 @@ 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 invalid_axes_limits():
4978+
with pytest.raises(ValueError) as err:
4979+
plt.set_xlim(left=np.nan)
4980+
with pytest.raises(ValueError) as err:
4981+
plt.set_xlim(left=np.inf)
4982+
with pytest.raises(ValueError) as err:
4983+
plt.set_xlim(right=np.nan)
4984+
with pytest.raises(ValueError) as err:
4985+
plt.set_xlim(right=np.inf)
4986+
4987+
with pytest.raises(ValueError) as err:
4988+
plt.set_ylim(bottom=np.nan)
4989+
with pytest.raises(ValueError) as err:
4990+
plt.set_ylim(bottom=np.inf)
4991+
with pytest.raises(ValueError) as err:
4992+
plt.set_ylim(top=np.nan)
4993+
with pytest.raises(ValueError) as err:
4994+
plt.set_ylim(top=np.inf)
4995+
4996+
err.match('NaN or Inf cannot be the argument values')

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,14 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
604604

605605
self._process_unit_info(xdata=(left, right))
606606
if left is not None:
607+
if (isinstance(left, float) and
608+
(not np.isreal(left) or not np.isfinite(left))):
609+
raise ValueError("NaN or Inf cannot be the argument values")
607610
left = self.convert_xunits(left)
608611
if right is not None:
612+
if (isinstance(right, float) and
613+
(not np.isreal(right) or not np.isfinite(right))):
614+
raise ValueError("NaN or Inf cannot be the argument values")
609615
right = self.convert_xunits(right)
610616

611617
old_left, old_right = self.get_xlim()
@@ -658,8 +664,14 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
658664

659665
self._process_unit_info(ydata=(bottom, top))
660666
if bottom is not None:
667+
if (isinstance(bottom, float) and
668+
(not np.isreal(bottom) or not np.isfinite(bottom))):
669+
raise ValueError("NaN or Inf cannot be the argument values")
661670
bottom = self.convert_yunits(bottom)
662671
if top is not None:
672+
if (isinstance(top, float) and
673+
(not np.isreal(top) or not np.isfinite(top))):
674+
raise ValueError("NaN or Inf cannot be the argument values")
663675
top = self.convert_yunits(top)
664676

665677
old_bottom, old_top = self.get_ylim()
@@ -712,8 +724,14 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
712724

713725
self._process_unit_info(zdata=(bottom, top))
714726
if bottom is not None:
727+
if (isinstance(bottom, float) and
728+
(not np.isreal(bottom) or not np.isfinite(bottom))):
729+
raise ValueError("NaN or Inf cannot be the argument values")
715730
bottom = self.convert_zunits(bottom)
716731
if top is not None:
732+
if (isinstance(top, float) and
733+
(not np.isreal(top) or not np.isfinite(top))):
734+
raise ValueError("NaN or Inf cannot be the argument values")
717735
top = self.convert_zunits(top)
718736

719737
old_bottom, old_top = self.get_zlim()

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,34 @@ def test_autoscale():
491491
ax.set_autoscalez_on(True)
492492
ax.plot([0, 2], [0, 2], [0, 2])
493493
assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.4, 2.4)
494+
495+
496+
def invalid_axes_limits():
497+
with pytest.raises(ValueError) as err:
498+
plt.set_xlim3d(left=np.nan)
499+
with pytest.raises(ValueError) as err:
500+
plt.set_xlim3d(left=np.inf)
501+
with pytest.raises(ValueError) as err:
502+
plt.set_xlim3d(right=np.nan)
503+
with pytest.raises(ValueError) as err:
504+
plt.set_xlim3d(right=np.inf)
505+
506+
with pytest.raises(ValueError) as err:
507+
plt.set_ylim3d(bottom=np.nan)
508+
with pytest.raises(ValueError) as err:
509+
plt.set_ylim3d(bottom=np.inf)
510+
with pytest.raises(ValueError) as err:
511+
plt.set_ylim3d(top=np.nan)
512+
with pytest.raises(ValueError) as err:
513+
plt.set_ylim3d(top=np.inf)
514+
515+
with pytest.raises(ValueError) as err:
516+
plt.set_zlim3d(bottom=np.nan)
517+
with pytest.raises(ValueError) as err:
518+
plt.set_zlim3d(bottom=np.inf)
519+
with pytest.raises(ValueError) as err:
520+
plt.set_zlim3d(top=np.nan)
521+
with pytest.raises(ValueError) as err:
522+
plt.set_zlim3d(top=np.inf)
523+
524+
err.match('NaN or Inf cannot be the argument values')

0 commit comments

Comments
 (0)