8000 Deduplicate implementations of FooNorm.autoscale{,_None} · matplotlib/matplotlib@c002bd6 · GitHub
[go: up one dir, main page]

Skip to content

Commit c002bd6

Browse files
committed
Deduplicate implementations of FooNorm.autoscale{,_None}
... and some docstring cleanups.
1 parent f4a7fea commit c002bd6

File tree

1 file changed

+20
-70
lines changed

1 file changed

+20
-70
lines changed

lib/matplotlib/colors.py

Lines changed: 20 additions & 70 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,10 @@ 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+
super().autoscale(np.ma.masked_less_equal(A, 0, copy=False))
10561048

10571049
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()
1050+
super().autoscale_None(np.ma.masked_less_equal(A, 0, copy=False))
10661051

10671052

10681053
class SymLogNorm(Normalize):
@@ -1124,9 +1109,7 @@ def __call__(self, value, clip=None):
11241109
return result
11251110

11261111
def _transform(self, a):
1127-
"""
1128-
Inplace transformation.
1129-
"""
1112+
"""Inplace transformation."""
11301113
with np.errstate(invalid="ignore"):
11311114
masked = np.abs(a) > self.linthresh
11321115
sign = np.sign(a[masked])
@@ -1137,9 +1120,7 @@ def _transform(self, a):
11371120
return a
11381121

11391122
def _inv_transform(self, a):
1140-
"""
1141-
Inverse inplace Transformation.
1142-
"""
1123+
"""Inverse inplace Transformation."""
11431124
masked = np.abs(a) > (self.linthresh * self._linscale_adj)
11441125
sign = np.sign(a[masked])
11451126
exp = np.exp(sign * a[masked] / self.linthresh - self._linscale_adj)
@@ -1149,9 +1130,7 @@ def _inv_transform(self, a):
11491130
return a
11501131

11511132
def _transform_vmin_vmax(self):
1152-
"""
1153-
Calculates vmin and vmax in the transformed system.
1154-
"""
1133+
"""Calculates vmin and vmax in the transformed system."""
11551134
vmin, vmax = self.vmin, self.vmax
11561135
arr = np.array([vmax, vmin]).astype(float)
11571136
self._upper, self._lower = self._transform(arr)
@@ -1164,22 +1143,11 @@ def inverse(self, value):
11641143
return self._inv_transform(val)
11651144

11661145
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)
1146+
super().autoscale(A)
11721147
self._transform_vmin_vmax()
11731148

11741149
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()
1150+
super().autoscale_None(A)
11831151
self._transform_vmin_vmax()
11841152

11851153

@@ -1233,34 +1201,17 @@ def inverse(self, value):
12331201
else:
12341202
return pow(value, 1. / gamma) * (vmax - vmin) + vmin
12351203

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-
12511204

12521205
class BoundaryNorm(Normalize):
12531206
"""
12541207
Generate a colormap index based on discrete intervals.
12551208
1256-
Unlike :class:`Normalize` or :class:`LogNorm`,
1257-
:class:`BoundaryNorm` maps values to integers instead of to the
1258-
interval 0-1.
1209+
Unlike `Normalize` or `LogNorm`, `BoundaryNorm` maps values to integers
1210+
instead of to the interval 0-1.
12591211
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.
1212+
Mapping to the 0-1 interval could have been done via piece-wise linear
1213+
interpolation, but using integers seems simpler, and reduces the number of
1214+
conversions back and forth between integer and floating point.
12641215
"""
12651216
def __init__(self, boundaries, ncolors, clip=False):
12661217
"""
@@ -1337,9 +1288,8 @@ def inverse(self, value):
13371288

13381289
class NoNorm(Normalize):
13391290
"""
1340-
Dummy replacement for Normalize, for the case where we
1341-
want to use indices directly in a
1342-
:class:`~matplotlib.cm.ScalarMappable` .
1291+
Dummy replacement for Normalize, for the case where we want to use indices
1292+
directly in a `~matplotlib.cm.ScalarMappable`.
13431293
"""
13441294
def __call__(self, value, clip=None):
13451295
return value

0 commit comments

Comments
 (0)
0