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

Skip to content

Commit 04ebb0d

Browse files
author
Vedant Nanda
committed
parametrized tests and cleaner calls to validate
1 parent 44fcb87 commit 04ebb0d

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,19 @@ def _determine_lims(self, xmin=None, xmax=None, *args, **kwargs):
585585
xmax += 0.05
586586
return (xmin, xmax)
587587

588+
def _validate_axis_limits(self, limit, convert):
589+
"""
590+
If the axis limits being set are infinite, this function
591+
592+
raises an error.
593+
594+
"""
595+
if limit is not None:
596+
if (isinstance(limit, float) and
597+
(not np.isreal(limit) or not np.isfinite(limit))):
598+
raise ValueError("NaN or Inf cannot be the argument values")
599+
return convert(limit)
600+
588601
def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
589602
"""
590603
Set 3D x limits.
@@ -603,10 +616,8 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
603616
left, right = left
604617

605618
self._process_unit_info(xdata=(left, right))
606-
if left is not None:
607-
left = self.convert_xunits(left)
608-
if right is not None:
609-
right = self.convert_xunits(right)
619+
left = self._validate_axis_limits(left, self.convert_xunits)
620+
right = self._validate_axis_limits(right, self.convert_xunits)
610621

611622
old_left, old_right = self.get_xlim()
612623
if left is None:
@@ -663,10 +674,8 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
663674
top = self.convert_yunits(top)
664675

665676
old_bottom, old_top = self.get_ylim()
666-
if bottom is None:
667-
bottom = old_bottom
668-
if top is None:
669-
top = old_top
677+
bottom = self._validate_axis_limits(bottom, self.convert_yunits)
678+
top = self._validate_axis_limits(top, self.convert_yunits)
670679

671680
if top == bottom:
672681
warnings.warn(('Attempting to set identical bottom==top results\n'
@@ -711,10 +720,8 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
711720
bottom, top = bottom
712721

713722
self._process_unit_info(zdata=(bottom, top))
714-
if bottom is not None:
715-
bottom = self.convert_zunits(bottom)
716-
if top is not None:
717-
top = self.convert_zunits(top)
723+
bottom = self._validate_axis_limits(bottom, self.convert_yunits)
724+
top = self._validate_axis_limits(top, self.convert_yunits)
718725

719726
old_bottom, old_top = self.get_zlim()
720727
if bottom is None:

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,20 @@ def test_autoscale():
513513
ax.set_autoscalez_on(True)
514514
ax.plot([0, 2], [0, 2], [0, 2])
515515
assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.4, 2.4)
516+
517+
518+
@pytest.mark.parametrize('value', [np.inf, np.nan])
519+
@pytest.mark.parametrize(('setter', 'side'), [
520+
('set_xlim3d', 'left'),
521+
('set_xlim3d', 'right'),
522+
('set_ylim3d', 'bottom'),
523+
('set_ylim3d', 'top'),
524+
('set_zlim3d', 'bottom'),
525+
('set_zlim3d', 'top'),
526+
])
527+
def test_invalid_axes_limits(setter, side, value):
528+
limit = {side: value}
529+
with pytest.raises(ValueError):
530+
fig = plt.figure()
531+
obj = fig.add_subplot(111, projection='3d')
532+
getattr(obj, setter)(**limit)

0 commit comments

Comments
 (0)