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

Skip to content

Commit d127a0c

Browse files
author
Vedant Nanda
committed
Raised error while setting invalid axes limits
1 parent eccf830 commit d127a0c

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
@@ -2871,11 +2871,13 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
28712871

28722872
self._process_unit_info(xdata=(left, right))
28732873
if left is not None:
2874-
if isinstance(left,float) and (math.isnan(left) or np.isinf(left)):
2874+
if (isinstance(left, float) and
2875+
(not np.isreal(left) or not np.isfinite(left))):
28752876
raise ValueError("NaN or Inf cannot be the argument values")
28762877
left = self.convert_xunits(left)
28772878
if right is not None:
2878-
if isinstance(right,float) and (math.isnan(right) or np.isinf(right)):
2879+
if (isinstance(right, float) and
2880+
(not np.isreal(right) or not np.isfinite(right))):
28792881
raise ValueError("NaN or Inf cannot be the argument values")
28802882
right = self.convert_xunits(right)
28812883

@@ -3169,8 +3171,14 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
31693171
bottom, top = bottom
31703172

31713173
if bottom is not None:
3174+
if (isinstance(bottom, float) and
3175+
(not np.isreal(bottom) or not np.isfinite(bottom))):
3176+
raise ValueError("NaN or Inf cannot be the argument values")
31723177
bottom = self.convert_yunits(bottom)
31733178
if top is not None:
3179+
if (isinstance(top, float) and
3180+
(not np.isreal(top) or not np.isfinite(top))):
3181+
raise ValueError("NaN or Inf cannot be the argument values")
31743182
top = self.convert_yunits(top)
31753183

31763184
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
@@ -4931,3 +4931,25 @@ 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 invalid_axes_limits():
4937+
with pytest.raises(ValueError) as err:
4938+
plt.set_xlim(left=np.nan)
4939+
with pytest.raises(ValueError) as err:
4940+
plt.set_xlim(left=np.inf)
4941+
with pytest.raises(ValueError) as err:
4942+
plt.set_xlim(right=np.nan)
4943+
with pytest.raises(ValueError) as err:
4944+
plt.set_xlim(right=np.inf)
4945+
4946+
with pytest.raises(ValueError) as err:
4947+
plt.set_ylim(bottom=np.nan)
4948+
with pytest.raises(ValueError) as err:
4949+
plt.set_ylim(bottom=np.inf)
4950+
with pytest.raises(ValueError) as err:
4951+
plt.set_ylim(top=np.nan)
4952+
with pytest.raises(ValueError) as err:
4953+
plt.set_ylim(top=np.inf)
4954+
4955+
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
@@ -475,3 +475,34 @@ def test_autoscale():
475475
ax.set_autoscalez_on(True)
476476
ax.plot([0, 2], [0, 2], [0, 2])
477477
assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.4, 2.4)
478+
479+
480+
def invalid_axes_limits():
481+
with pytest.raises(ValueError) as err:
482+
plt.set_xlim3d(left=np.nan)
483+
with pytest.raises(ValueError) as err:
484+
plt.set_xlim3d(left=np.inf)
485+
with pytest.raises(ValueError) as err:
486+
plt.set_xlim3d(right=np.nan)
487+
with pytest.raises(ValueError) as err:
488+
plt.set_xlim3d(right=np.inf)
489+
490+
with pytest.raises(ValueError) as err:
491+
plt.set_ylim3d(bottom=np.nan)
492+
with pytest.raises(ValueError) as err:
493+
plt.set_ylim3d(bottom=np.inf)
494+
with pytest.raises(ValueError) as err:
495+
plt.set_ylim3d(top=np.nan)
496+
with pytest.raises(ValueError) as err:
497+
plt.set_ylim3d(top=np.inf)
498+
499+
with pytest.raises(ValueError) as err:
500+
plt.set_zlim3d(bottom=np.nan)
501+
with pytest.raises(ValueError) as err:
502+
plt.set_zlim3d(bottom=np.inf)
503+
with pytest.raises(ValueError) as err:
504+
plt.set_zlim3d(top=np.nan)
505+
with pytest.raises(ValueError) as err:
506+
plt.set_zlim3d(top=np.inf)
507+
508+
err.match('NaN or Inf cannot be the argument values')

0 commit comments

Comments
 (0)