8000 Deduplicate implementations of FooNorm.autoscale{,_None} (#12309) · matplotlib/matplotlib@6eee9ce · GitHub
[go: up one dir, main page]

Skip to content

Commit 6eee9ce

Browse files
anntzertimhoffm
authored andcommitted
Deduplicate implementations of FooNorm.autoscale{,_None} (#12309)
... and some docstring cleanups.
1 parent e7b8533 commit 6eee9ce

File tree

1 file changed

+25
-71
lines changed

1 file changed

+25
-71
lines changed

lib/matplotlib/colors.py

Lines changed: 25 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -974,30 +974,27 @@ def inverse(self, value):
974974
return vmin + value * (vmax - vmin)
975975

976976
def autoscale(self, A):
977-
"""
978-
Set *vmin*, *vmax* to min, max of *A*.
979-
"""
977+
"""Set *vmin*, *vmax* to min, max of *A*."""
980978
A = np.asanyarray(A)
981979
self.vmin = A.min()
982980
self.vmax = A.max()
983981

984982
def autoscale_None(self, A):
985-
"""autoscale only None-valued vmin or vmax."""
983+
"""Autoscale only None-valued vmin or vmax."""
986984
A = np.asanyarray(A)
987985
if self.vmin is None and A.size:
988986
self.vmin = A.min()
989987
if self.vmax is None and A.size:
990988
self.vmax = A.max()
991989

992990
def scaled(self):
993-
'return true if vmin and vmax set'
994-
return (self.vmin is not None and self.vmax is not None)
991+
"""Return whether vmin and vmax are set."""
992+
return self.vmin is not None and self.vmax is not None
995993

996994

997995
class LogNorm(Normalize):
998-
"""
999-
Normalize a given value to the 0-1 range on a log scale
1000-
"""
996+
"""Normalize a given value to the 0-1 range on a log scale."""
997+
1001998
def __call__(self, value, clip=None):
1002999
if clip is None:
10031000
clip = self.clip
@@ -1047,22 +1044,12 @@ def inverse(self, value):
10471044
return vmin * pow((vmax / vmin), value)
10481045

10491046
def autoscale(self, A):
1050-
"""
1051-
Set *vmin*, *vmax* to min, max of *A*.
1052-
"""
1053-
A = np.ma.masked_less_equal(A, 0, copy=False)
1054-
self.vmin = np.ma.min(A)
1055-
self.vmax = np.ma.max(A)
1047+
# docstring inherited.
1048+
super().autoscale(np.ma.masked_less_equal(A, 0, copy=False))
10561049

10571050
def autoscale_None(self, A):
1058-
"""autoscale only None-valued vmin or vmax."""
1059-
if self.vmin is not None and self.vmax is not None:
1060-
return
1061-
A = np.ma.masked_less_equal(A, 0, copy=False)
1062-
if self.vmin is None and A.size:
1063-
self.vmin = A.min()
1064-
if self.vmax is None and A.size:
1065-
self.vmax = A.max()
1051+
# docstring inherited.
1052+
super().autoscale_None(np.ma.masked_less_equal(A, 0, copy=False))
10661053

10671054

10681055
class SymLogNorm(Normalize):
@@ -1124,9 +1111,7 @@ def __call__(self, value, clip=None):
11241111
return result
11251112

11261113
def _transform(self, a):
1127-
"""
1128-
Inplace transformation.
1129-
"""
1114+
"""Inplace transformation."""
11301115
with np.errstate(invalid="ignore"):
11311116
masked = np.abs(a) > self.linthresh
11321117
sign = np.sign(a[masked])
@@ -1137,9 +1122,7 @@ def _transform(self, a):
11371122
return a
11381123

11391124
def _inv_transform(self, a):
1140-
"""
1141-
Inverse inplace Transformation.
1142-
"""
1125+
"""Inverse inplace Transformation."""
11431126
masked = np.abs(a) > (self.linthresh * self._linscale_adj)
11441127
sign = np.sign(a[masked])
11451128
exp = np.exp(sign * a[masked] / self.linthresh - self._linscale_adj)
@@ -1149,9 +1132,7 @@ def _inv_transform(self, a):
11491132
return a
11501133

11511134
def _transform_vmin_vmax(self):
1152-
"""
1153-
Calculates vmin and vmax in the transformed system.
1154-
"""
1135+
"""Calculates vmin and vmax in the transformed system."""
11551136
vmin, vmax = self.vmin, self.vmax
11561137
arr = np.array([vmax, vmin]).astype(float)
11571138
self._upper, self._lower = self._transform(arr)
@@ -1164,22 +1145,13 @@ def inverse(self, value):
11641145
return self._inv_transform(val)
11651146

11661147
def autoscale(self, A):
1167-
"""
1168-
Set *vmin*, *vmax* to min, max of *A*.
1169-
"""
1170-
self.vmin = np.ma.min(A)
1171-
self.vmax = np.ma.max(A)
1148+
# docstring inherited.
1149+
super().autoscale(A)
11721150
self._transform_vmin_vmax()
11731151

11741152
def autoscale_None(self, A):
1175-
"""autoscale only None-valued vmin or vmax."""
1176-
if self.vmin is not None and self.vmax is not None:
1177-
pass
1178-
A = np.asanyarray(A)
1179-
if self.vmin is None and A.size:
1180-
self.vmin = A.min()
1181-
if self.vmax is None and A.size:
1182-
self.vmax = A.max()
1153+
# docstring inherited.
1154+
super().autoscale_None(A)
11831155
self._transform_vmin_vmax()
11841156

11851157

@@ -1233,34 +1205,17 @@ def inverse(self, value):
12331205
else:
12341206
return pow(value, 1. / gamma) * (vmax - vmin) + vmin
12351207

1236-
def autoscale(self, A):
1237-
"""
1238-
Set *vmin*, *vmax* to min, max of *A*.
1239-
"""
1240-
self.vmin = np.ma.min(A)
1241-
self.vmax = np.ma.max(A)
1242-
1243-
def autoscale_None(self, A):
1244-
"""autoscale only None-valued vmin or vmax."""
1245-
A = np.asanyarray(A)
1246-
if self.vmin is None and A.size:
1247-
self.vmin = A.min()
1248-
if self.vmax is None and A.size:
1249-
self.vmax = A.max()
1250-
12511208

12521209
class BoundaryNorm(Normalize):
12531210
"""
12541211
Generate a colormap index based on discrete intervals.
12551212
1256-
Unlike :class:`Normalize` or :class:`LogNorm`,
1257-
:class:`BoundaryNorm` maps values to integers instead of to the
1258-
interval 0-1.
1213+
Unlike `Normalize` or `LogNorm`, `BoundaryNorm` maps values to integers
1214+
instead of to the interval 0-1.
12591215
1260-
Mapping to the 0-1 interval could have been done via
1261-
piece-wise linear interpolation, but using integers seems
1262-
simpler, and reduces the number of conversions back and forth
1263-
between integer and floating point.
1216+
Mapping to the 0-1 interval could have been done via piece-wise linear
1217+
interpolation, but using integers seems simpler, and reduces the number of
1218+
conversions back and forth between integer and floating point.
12641219
"""
12651220
def __init__(self, boundaries, ncolors, clip=False):
12661221
"""
@@ -1337,9 +1292,8 @@ def inverse(self, value):
13371292

13381293
class NoNorm(Normalize):
13391294
"""
1340-
Dummy replacement for Normalize, for the case where we
1341-
want to use indices directly in a
1342-
:class:`~matplotlib.cm.ScalarMappable` .
1295+
Dummy replacement for `Normalize`, for the case where we want to use
1296+
indices directly in a `~matplotlib.cm.ScalarMappable`.
13431297
"""
13441298
def __call__(self, value, clip=None):
13451299
return value
@@ -1350,7 +1304,7 @@ def inverse(self, value):
13501304

13511305
def rgb_to_hsv(arr):
13521306
"""
1353-
convert float rgb values (in the r 4DDA ange [0, 1]), in a numpy array to hsv
1307+
Convert float rgb values (in the range [0, 1]), in a numpy array to hsv
13541308
values.
13551309
13561310
Parameters

0 commit comments

Comments
 (0)
0