Skip to content

Commit c3df005

Browse files
committed
scale: add ScaleBase.val_in_range for domain validation
Introduce a val_in_range method in ScaleBase to explicitly check whether values lie within the valid domain of a scale. The default implementation falls back to limit_range_for_scale for compatibility with existing scales. Specific scales (e.g., LogScale, LogitScale) override this method with more efficient checks.
1 parent 32805b1 commit c3df005

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

lib/matplotlib/scale.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ 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+
117+
def val_in_range(self, val):
118+
"""
119+
Return whether the value(s) are within the valid range for this scale.
120+
"""
121+
if np.isscalar(val):
122+
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])
116127

117128

118129
def _make_axis_parameter_optional(init_func):
@@ -195,6 +206,13 @@ def get_transform(self):
195206
`~matplotlib.transforms.IdentityTransform`.
196207
"""
197208
return IdentityTransform()
209+
210+
def val_in_range(self, val):
211+
"""
212+
Return `True` for all values.
213+
"""
214+
val = np.asanyarray(val)
215+
return np.ones(val.shape, dtype=bool)
198216

199217

200218
class FuncTransform(Transform):
@@ -399,6 +417,10 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
399417

400418
return (minpos if vmin <= 0 else vmin,
401419
minpos if vmax <= 0 else vmax)
420+
421+
def val_in_range(self, val):
422+
"""Return `True` for positive values."""
423+
return np.asanyarray(val) > 0
402424

403425

404426
class FuncScaleLog(LogScale):
@@ -819,6 +841,13 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
819841
minpos = 1e-7 # Should rarely (if ever) have a visible effect.
820842
return (minpos if vmin <= 0 else vmin,
821843
1 - minpos if vmax >= 1 else vmax)
844+
845+
def val_in_range(self, val):
846+
"""
847+
Return `True` if value(s) lie between 0 and 1 (excluded)
848+
"""
849+
val = np.asanyarray(val)
850+
return (val > 0) & (val < 1)
822851

823852

824853
_scale_mapping = {

0 commit comments

Comments
 (0)