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

Skip to content

Commit 1c0b96c

Browse files
authored
Merge pull request #8474 from nvedant07/infinite_limits
Check for non-finite axis limits placed on converted_limit
2 parents 7ddd6b0 + 1667fb5 commit 1c0b96c

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,6 +2810,25 @@ def get_xlim(self):
28102810
"""
28112811
return tuple(self.viewLim.intervalx)
28122812

2813+
def _validate_converted_limits(self, limit, convert):
2814+
"""
2815+
Raise ValueError if converted limits are non-finite.
2816+
2817+
Note that this function also accepts None as a limit argument.
2818+
2819+
Returns
2820+
-------
2821+
The limit value after call to convert(), or None if limit is None.
2822+
2823+
"""
2824+
if limit is not None:
2825+
converted_limit = convert(limit)
2826+
if (isinstance(converted_limit, float) and
2827+
(not np.isreal(converted_limit) or
2828+
not np.isfinite(converted_limit))):
2829+
raise ValueError("Axis limits cannot be NaN or Inf")
2830+
return converted_limit
2831+
28132832
def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
28142833
"""
28152834
Set the data limits for the x-axis
@@ -2876,15 +2895,8 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
28762895
left, right = left
28772896

28782897
self._process_unit_info(xdata=(left, right))
2879-
if left is not None:
2880-
left = self.convert_xunits(left)
2881-
if right is not None:
2882-
right = self.convert_xunits(right)
2883-
2884-
if ((left is not None and not np.isfinite(left)) or
2885-
(right is not None and not np.isfinite(right))):
2886-
raise ValueError("Specified x limits must be finite; "
2887-
"instead, found: (%s, %s)" % (left, right))
2898+
left = self._validate_converted_limits(left, self.convert_xunits)
2899+
right = self._validate_converted_limits(right, self.convert_xunits)
28882900

28892901
old_left, old_right = self.get_xlim()
28902902
if left is None:
@@ -3175,15 +3187,8 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
31753187
if top is None and iterable(bottom):
31763188
bottom, top = bottom
31773189

3178-
if bottom is not None:
3179-
bottom = self.convert_yunits(bottom)
3180-
if top is not None:
3181-
top = self.convert_yunits(top)
3182-
3183-
if ((top is not None and not np.isfinite(top)) or
3184-
(bottom is not None and not np.isfinite(bottom))):
3185-
raise ValueError("Specified y limits must be finite; "
3186-
"instead, found: (%s, %s)" % (bottom, top))
3190+
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
3191+
top = self._validate_converted_limits(top, self.convert_yunits)
31873192

31883193
old_bottom, old_top = self.get_ylim()
31893194

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -587,18 +587,6 @@ 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-
602590
def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
603591
"""
604592
Set 3D x limits.
@@ -617,8 +605,8 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
617605
left, right = left
618606

619607
self._process_unit_info(xdata=(left, right))
620-
left = self._validate_axis_limits(left, self.convert_xunits)
621-
right = self._validate_axis_limits(right, self.convert_xunits)
608+
left = self._validate_converted_limits(left, self.convert_xunits)
609+
right = self._validate_converted_limits(right, self.convert_xunits)
622610

623611
old_left, old_right = self.get_xlim()
624612
if left is None:
@@ -669,14 +657,14 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
669657
bottom, top = bottom
670658

671659
self._process_unit_info(ydata=(bottom, top))
672-
if bottom is not None:
673-
bottom = self.convert_yunits(bottom)
674-
if top is not None:
675-
top = self.convert_yunits(top)
660+
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
661+
top = self._validate_converted_limits(top, self.convert_yunits)
676662

677663
old_bottom, old_top = self.get_ylim()
678-
bottom = self._validate_axis_limits(bottom, self.convert_yunits)
679-
top = self._validate_axis_limits(top, self.convert_yunits)
664+
if bottom is None:
665+
bottom = old_bottom
666+
if top is None:
667+
top = old_top
680668

681669
if top == bottom:
682670
warnings.warn(('Attempting to set identical bottom==top results\n'
@@ -721,8 +709,8 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
721709
bottom, top = bottom
722710

723711
self._process_unit_info(zdata=(bottom, top))
724-
bottom = self._validate_axis_limits(bottom, self.convert_yunits)
725-
top = self._validate_axis_limits(top, self.convert_yunits)
712+
bottom = self._validate_converted_limits(bottom, self.convert_zunits)
713+
top = self._validate_converted_limits(top, self.convert_zunits)
726714

727715
old_bottom, old_top = self.get_zlim()
728716
if bottom is None:

0 commit comments

Comments
 (0)