Skip to content

Commit e480331

Browse files
authored
Merge pull request #8022 from nvedant07/first_try
Fixed Issue #7460: Raised an error if argument to xlim is invalid
2 parents b81576c + 187f5da commit e480331

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

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

590+
def _validate_axis_limits(self, limit, convert):
591+
"""
592+
Raise ValueError if specified axis limits are infinite.
593+
594+
"""
595+
if limit is not None:
596+
converted_limits = convert(limit)
597+
if (isinstance(limit, float) and
598+
(not np.isreal(limit) or not np.isfinite(limit))):
599+
raise ValueError("Axis limits cannot be NaN or Inf")
600+
return converted_limits
601+
590602
def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
591603
"""
592604
Set 3D x limits.
@@ -605,10 +617,8 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
605617
left, right = left
606618

607619
self._process_unit_info(xdata=(left, right))
608-
if left is not None:
609-
left = self.convert_xunits(left)
610-
if right is not None:
611-
right = self.convert_xunits(right)
620+
left = self._validate_axis_limits(left, self.convert_xunits)
621+
right = self._validate_axis_limits(right, self.convert_xunits)
612622

613623
old_left, old_right = self.get_xlim()
614624
if left is None:
@@ -665,10 +675,8 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
665675
top = self.convert_yunits(top)
666676

667677
old_bottom, old_top = self.get_ylim()
668-
if bottom is None:
669-
bottom = old_bottom
670-
if top is None:
671-
top = old_top
678+
bottom = self._validate_axis_limits(bottom, self.convert_yunits)
679+
top = self._validate_axis_limits(top, self.convert_yunits)
672680

673681
if top == bottom:
674682
warnings.warn(('Attempting to set identical bottom==top results\n'
@@ -713,10 +721,8 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
713721
bottom, top = bottom
714722

715723
self._process_unit_info(zdata=(bottom, top))
716-
if bottom is not None:
717-
bottom = self.convert_zunits(bottom)
718-
if top is not None:
719-
top = self.convert_zunits(top)
724+
bottom = self._validate_axis_limits(bottom, self.convert_yunits)
725+
top = self._validate_axis_limits(top, self.convert_yunits)
720726

721727
old_bottom, old_top = self.get_zlim()
722728
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
@@ -547,3 +547,20 @@ def test_axes3d_ortho():
547547
fig = plt.figure()
548548
ax = fig.gca(projection='3d')
549549
ax.set_proj_type('ortho')
550+
551+
552+
@pytest.mark.parametrize('value', [np.inf, np.nan])
553+
@pytest.mark.parametrize(('setter', 'side'), [
554+
('set_xlim3d', 'left'),
555+
('set_xlim3d', 'right'),
556+
('set_ylim3d', 'bottom'),
557+
('set_ylim3d', 'top'),
558+
('set_zlim3d', 'bottom'),
559+
('set_zlim3d', 'top'),
560+
])
561+
def test_invalid_axes_limits(setter, side, value):
562+
limit = {side: value}
563+
fig = plt.figure()
564+
obj = fig.add_subplot(111, projection='3d')
565+
with pytest.raises(ValueError):
566+
getattr(obj, setter)(**limit)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy