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

Skip to content

Check for non-finite axis limits placed on converted_limit #8474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2810,6 +2810,25 @@ def get_xlim(self):
"""
return tuple(self.viewLim.intervalx)

def _validate_converted_limits(self, limit, convert):
"""
Raise ValueError if converted limits are non-finite.

Note that this function also accepts None as a limit argument.

Returns
-------
The limit value after call to convert(), or None if limit is None.

"""
if limit is not None:
converted_limit = convert(limit)
if (isinstance(converted_limit, float) and
(not np.isreal(converted_limit) or
not np.isfinite(converted_limit))):
raise ValueError("Axis limits cannot be NaN or Inf")
return converted_limit

def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
"""
Set the data limits for the x-axis
Expand Down Expand Up @@ -2876,15 +2895,8 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
left, right = left

self._process_unit_info(xdata=(left, right))
if left is not None:
left = self.convert_xunits(left)
if right is not None:
right = self.convert_xunits(right)

if ((left is not None and not np.isfinite(left)) or
(right is not None and not np.isfinite(right))):
raise ValueError("Specified x limits must be finite; "
"instead, found: (%s, %s)" % (left, right))
left = self._validate_converted_limits(left, self.convert_xunits)
right = self._validate_converted_limits(right, self.convert_xunits)

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

if bottom is not None:
bottom = self.convert_yunits(bottom)
if top is not None:
top = self.convert_yunits(top)

if ((top is not None and not np.isfinite(top)) or
(bottom is not None and not np.isfinite(bottom))):
raise ValueError("Specified y limits must be finite; "
"instead, found: (%s, %s)" % (bottom, top))
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
top = self._validate_converted_limits(top, self.convert_yunits)

old_bottom, old_top = self.get_ylim()

Expand Down
32 changes: 10 additions & 22 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,6 @@ def _determine_lims(self, xmin=None, xmax=None, *args, **kwargs):
xmax += 0.05
return (xmin, xmax)

def _validate_axis_limits(self, limit, convert):
"""
Raise ValueError if specified axis limits are infinite.

"""
if limit is not None:
converted_limits = convert(limit)
if (isinstance(limit, float) and
(not np.isreal(limit) or not np.isfinite(limit))):
raise ValueError("Axis limits cannot be NaN or Inf")
return converted_limits

def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
"""
Set 3D x limits.
Expand All @@ -617,8 +605,8 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
left, right = left

self._process_unit_info(xdata=(left, right))
left = self._validate_axis_limits(left, self.convert_xunits)
right = self._validate_axis_limits(right, self.convert_xunits)
left = self._validate_converted_limits(left, self.convert_xunits)
right = self._validate_converted_limits(right, self.convert_xunits)

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

self._process_unit_info(ydata=(bottom, top))
if bottom is not None:
bottom = self.convert_yunits(bottom)
if top is not None:
top = self.convert_yunits(top)
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
top = self._validate_converted_limits(top, self.convert_yunits)

old_bottom, old_top = self.get_ylim()
bottom = self._validate_axis_limits(bottom, self.convert_yunits)
top = self._validate_axis_limits(top, self.convert_yunits)
if bottom is None:
bottom = old_bottom
if top is None:
top = old_top

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

self._process_unit_info(zdata=(bottom, top))
bottom = self._validate_axis_limits(bottom, self.convert_yunits)
top = self._validate_axis_limits(top, self.convert_yunits)
bottom = self._validate_converted_limits(bottom, self.convert_zunits)
top = self._validate_converted_limits(top, self.convert_zunits)

old_bottom, old_top = self.get_zlim()
if bottom is None:
Expand Down