@@ -2810,6 +2810,19 @@ def get_xlim(self):
2810
2810
"""
2811
2811
return tuple (self .viewLim .intervalx )
2812
2812
2813
+ def _validate_axis_limits (self , limit , convert ):
2814
+ """
2815
+ Raise ValueError if specified axis limits are infinite.
2816
+
2817
+ """
2818
+ if limit is not None :
2819
+ converted_limit = convert (limit )
2820
+ if (isinstance (converted_limit , float ) and
2821
+ (not np .isreal (converted_limit ) or
2822
+ not np .isfinite (converted_limit ))):
2823
+ raise ValueError ("Axis limits cannot be NaN or Inf" )
2824
+ return converted_limit
2825
+
2813
2826
def set_xlim (self , left = None , right = None , emit = True , auto = False , ** kw ):
2814
2827
"""
2815
2828
Set the data limits for the x-axis
@@ -2876,15 +2889,8 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
2876
2889
left , right = left
2877
2890
2878
2891
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 ))
2892
+ left = self ._validate_axis_limits (left , self .convert_xunits )
2893
+ right = self ._validate_axis_limits (right , self .convert_xunits )
2888
2894
2889
2895
old_left , old_right = self .get_xlim ()
2890
2896
if left is None :
@@ -3175,15 +3181,8 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
3175
3181
if top is None and iterable (bottom ):
3176
3182
bottom , top = bottom
3177
3183
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 ))
3184
+ bottom = self ._validate_axis_limits (bottom , self .convert_yunits )
3185
+ top = self ._validate_axis_limits (top , self .convert_yunits )
3187
3186
3188
3187
old_bottom , old_top = self .get_ylim ()
3189
3188
0 commit comments