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

Skip to content

Commit 631a52f

Browse files
committed
scale: refactor val_in_range to accept scalar values and return a single bool
1 parent 6feeec1 commit 631a52f

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

lib/matplotlib/scale.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,19 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
113113
This is used by log scales to determine a minimum value.
114114
"""
115115
return vmin, vmax
116-
116+
117117
def val_in_range(self, val):
118118
"""
119119
Return whether the value(s) are within the valid range for this scale.
120+
121+
This method is a generic implementation. Subclasses may implement more
122+
efficient solutions for their domain.
120123
"""
121-
if np.isscalar(val):
124+
try:
122125
vmin, vmax = self.limit_range_for_scale(val, val, minpos=1e-300)
123-
return (vmax == val) and (vmin == val)
124-
125-
val = np.asanyarray(val)
126-
return np.array([self.val_in_range(v) for v in val])
126+
return vmin == val and vmax == val
127+
except (TypeError, ValueError):
128+
return False
127129

128130

129131
def _make_axis_parameter_optional(init_func):
@@ -206,13 +208,14 @@ def get_transform(self):
206208
`~matplotlib.transforms.IdentityTransform`.
207209
"""
208210
return IdentityTransform()
209-
211+
210212
def val_in_range(self, val):
211213
"""
212-
Return `True` for all values.
214+
Return whether the value is within the valid range for this scale.
215+
216+
This is True for all values.
213217
"""
214-
val = np.asanyarray(val)
215-
return np.ones(val.shape, dtype=bool)
218+
return True
216219

217220

218221
class FuncTransform(Transform):
@@ -417,10 +420,17 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
417420

418421
return (minpos if vmin <= 0 else vmin,
419422
minpos if vmax <= 0 else vmax)
420-
423+
421424
def val_in_range(self, val):
422-
"""Return `True` for positive values."""
423-
return np.asanyarray(val) > 0
425+
"""
426+
Return whether the value is within the valid range for this scale.
427+
428+
This is True for value(s) > 0
429+
"""
430+
if np.isnan(val):
431+
return False
432+
else:
433+
return val > 0
424434

425435

426436
class FuncScaleLog(LogScale):
@@ -841,13 +851,14 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
841851
minpos = 1e-7 # Should rarely (if ever) have a visible effect.
842852
return (minpos if vmin <= 0 else vmin,
843853
1 - minpos if vmax >= 1 else vmax)
844-
854+
845855
def val_in_range(self, val):
846856
"""
847-
Return `True` if value(s) lie between 0 and 1 (excluded)
857+
Return whether the value is within the valid range for this scale.
858+
859+
This is True for value(s) which are between 0 and 1 (excluded).
848860
"""
849-
val = np.asanyarray(val)
850-
return (val > 0) & (val < 1)
861+
return (val > 0) and (val < 1)
851862

852863

853864
_scale_mapping = {

0 commit comments

Comments
 (0)