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

Skip to content

Commit 93cab2e

Browse files
DOC: Clarify description and add examples in colors.Normalize (#26915)
Co-authored-by: Zihao Yang [email protected]
1 parent 31ae0fe commit 93cab2e

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

lib/matplotlib/colors.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,36 +1215,55 @@ def reversed(self, name=None):
12151215

12161216
class Normalize:
12171217
"""
1218-
A class which, when called, linearly normalizes data into the
1219-
``[0.0, 1.0]`` interval.
1218+
A class which, when called, maps values within the interval
1219+
``[vmin, vmax]`` linearly to the interval ``[0.0, 1.0]``. The mapping of
1220+
values outside ``[vmin, vmax]`` depends on *clip*.
1221+
1222+
Examples
1223+
--------
1224+
::
1225+
1226+
x = [-2, -1, 0, 1, 2]
1227+
1228+
norm = mpl.colors.Normalize(vmin=-1, vmax=1, clip=False)
1229+
norm(x) # [-0.5, 0., 0.5, 1., 1.5]
1230+
norm = mpl.colors.Normalize(vmin=-1, vmax=1, clip=True)
1231+
norm(x) # [0., 0., 0.5, 1., 1.]
1232+
1233+
See Also
1234+
--------
1235+
:ref:`colormapnorms`
12201236
"""
12211237

12221238
def __init__(self, vmin=None, vmax=None, clip=False):
12231239
"""
12241240
Parameters
12251241
----------
12261242
vmin, vmax : float or None
1227-
If *vmin* and/or *vmax* is not given, they are initialized from the
1228-
minimum and maximum value, respectively, of the first input
1229-
processed; i.e., ``__call__(A)`` calls ``autoscale_None(A)``.
1243+
Values within the range ``[vmin, vmax]`` from the input data will be
1244+
linearly mapped to ``[0, 1]``. If either *vmin* or *vmax* is not
1245+
provided, they default to the minimum and maximum values of the input,
1246+
respectively.
1247+
12301248
12311249
clip : bool, default: False
12321250
Determines the behavior for mapping values outside the range
12331251
``[vmin, vmax]``.
12341252
1235-
If clipping is off, values outside the range ``[vmin, vmax]`` are also
1236-
transformed linearly, resulting in values outside ``[0, 1]``. For a
1237-
standard use with colormaps, this behavior is desired because colormaps
1238-
mark these outside values with specific colors for *over* or *under*.
1253+
If *clip* is ``False``, values outside ``[vmin, vmax]`` are also transformed
1254+
linearly, leading to results outside ``[0, 1]``. For a standard use with
1255+
colormaps, this behavior is desired because colormaps mark these outside
1256+
values with specific colors for over or under.
12391257
1240-
If ``True`` values falling outside the range ``[vmin, vmax]``,
1241-
are mapped to 0 or 1, whichever is closer. This makes these values
1258+
If *clip* is ``True``, values outside ``[vmin, vmax]`` are set to 0 or 1,
1259+
depending on which boundary they're closer to. This makes these values
12421260
indistinguishable from regular boundary values and can lead to
12431261
misinterpretation of the data.
12441262
1263+
12451264
Notes
12461265
-----
1247-
Returns 0 if ``vmin == vmax``.
1266+
If ``vmin == vmax``, input data will be mapped to 0.
12481267
"""
12491268
self._vmin = _sanitize_extrema(vmin)
12501269
self._vmax = _sanitize_extrema(vmax)
@@ -1298,6 +1317,11 @@ def process_value(value):
12981317
12991318
*value* can be a scalar or sequence.
13001319
1320+
Parameters
1321+
----------
1322+
value
1323+
Data to normalize.
1324+
13011325
Returns
13021326
-------
13031327
result : masked array
@@ -1328,8 +1352,7 @@ def process_value(value):
13281352

13291353
def __call__(self, value, clip=None):
13301354
"""
1331-
Normalize *value* data in the ``[vmin, vmax]`` interval into the
1332-
``[0.0, 1.0]`` interval and return it.
1355+
Normalize the data and return the normalized data.
13331356
13341357
Parameters
13351358
----------
@@ -1375,6 +1398,15 @@ def __call__(self, value, clip=None):
13751398
return result
13761399

13771400
def inverse(self, value):
1401+
"""
1402+
Maps the normalized value (i.e., index in the colormap) back to image
1403+
data value.
1404+
1405+
Parameters
1406+
----------
1407+
value
1408+
Normalized value.
1409+
"""
13781410
if not self.scaled():
13791411
raise ValueError("Not invertible until both vmin and vmax are set")
13801412
(vmin,), _ = self.process_value(self.vmin)
@@ -1396,7 +1428,7 @@ def autoscale(self, A):
13961428
self._changed()
13971429

13981430
def autoscale_None(self, A):
1399-
"""If vmin or vmax are not set, use the min/max of *A* to set them."""
1431+
"""If *vmin* or *vmax* are not set, use the min/max of *A* to set them."""
14001432
A = np.asanyarray(A)
14011433

14021434
if isinstance(A, np.ma.MaskedArray):
@@ -1410,7 +1442,7 @@ def autoscale_None(self, A):
14101442
self.vmax = A.max()
14111443

14121444
def scaled(self):
1413-
"""Return whether vmin and vmax are set."""
1445+
"""Return whether *vmin* and *vmax* are both set."""
14141446
return self.vmin is not None and self.vmax is not None
14151447

14161448

0 commit comments

Comments
 (0)