Skip to content

Commit c966778

Browse files
committed
Simplify normalization of multiple images
1 parent 341070a commit c966778

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Added data argument to `.Normalize`
2+
-----------------------------------
3+
4+
`.Normalize` got a data keyword argument to easy generation of normalized
5+
plots. `Normalize(data=d)` is equivalent to
6+
`Normalize(vmin=np.min(d), vmax=np.max(d))`. For an example see
7+
:doc:`examples/images_contours_and_fields/multi_image.py`.

lib/matplotlib/colors.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ class Normalize(object):
859859
the ``[0.0, 1.0]`` interval.
860860
861861
"""
862-
def __init__(self, vmin=None, vmax=None, clip=False):
862+
def __init__(self, vmin=None, vmax=None, clip=False, *, data=None):
863863
"""
864864
If *vmin* or *vmax* is not given, they are initialized from the
865865
minimum and maximum value respectively of the first input
@@ -876,10 +876,16 @@ def __init__(self, vmin=None, vmax=None, clip=False):
876876
the over, under, and masked colors in the colormap, so it is
877877
likely to lead to surprises; therefore the default is
878878
*clip* = *False*.
879+
880+
You can pass an array-like object to *data*, e.g. a list of
881+
numpy arrays. If *vmin* or *vmax* are *None*, the respective min/max
882+
of the array is taken as *vmin* / *vmax*.
879883
"""
880884
self.vmin = _sanitize_extrema(vmin)
881885
self.vmax = _sanitize_extrema(vmax)
882886
self.clip = clip
887+
if data is not None:
888+
self.autoscale_None(data)
883889

884890
@staticmethod
885891
def process_value(value):
@@ -1061,8 +1067,8 @@ class SymLogNorm(Normalize):
10611067
*linthresh* allows the user to specify the size of this range
10621068
(-*linthresh*, *linthresh*).
10631069
"""
1064-
def __init__(self, linthresh, linscale=1.0,
1065-
vmin=None, vmax=None, clip=False):
1070+
def __init__(self, linthresh, linscale=1.0,
1071+
vmin=None, vmax=None, clip=False, *, data=None):
10661072
"""
10671073
*linthresh*:
10681074
The range within which the plot is linear (to
@@ -1076,11 +1082,15 @@ def __init__(self, linthresh, linscale=1.0,
10761082
default), the space used for the positive and negative
10771083
halves of the linear range will be equal to one decade in
10781084
the logarithmic range. Defaults to 1.
1085+
1086+
You can pass an array-like object to *data*, e.g. a list of
1087+
numpy arrays. If *vmin* or *vmax* are *None*, the respective min/max
1088+
of the array is taken as *vmin* / *vmax*.
10791089
"""
1080-
Normalize.__init__(self, vmin, vmax, clip)
1090+
Normalize.__init__(self, vmin, vmax, clip, data=data)
10811091
self.linthresh = float(linthresh)
10821092
self._linscale_adj = (linscale / (1.0 - np.e ** -1))
1083-
if vmin is not None and vmax is not None:
1093+
if self.vmin is not None and self.vmax is not None:
10841094
self._transform_vmin_vmax()
10851095

10861096
def __call__(self, value, clip=None):
@@ -1173,8 +1183,8 @@ class PowerNorm(Normalize):
11731183
Normalize a given value to the ``[0, 1]`` interval with a power-law
11741184
scaling. This will clip any negative data points to 0.
11751185
"""
1176-
def __init__(self, gamma, vmin=None, vmax=None, clip=False):
1177-
Normalize.__init__(self, vmin, vmax, clip)
1186+
def __init__(self, gamma, vmin=None, vmax=None, clip=False, *, data=None):
1187+
Normalize.__init__(self, vmin, vmax, clip, data=data)
11781188
self.gamma = gamma
11791189

11801190
def __call__(self, value, clip=None):

lib/matplotlib/tests/test_colors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ def test_Normalize():
212212
assert 0 < norm(1 + 50 * eps) < 1
213213

214214

215+
def test_Normalize_data():
216+
vals = np.linspace(-1, 2, 5)
217+
norm = mcolors.Normalize(data=vals)
218+
assert_array_equal([norm.vmin, norm.vmax], [vals.min(), vals.max()])
219+
220+
215221
def test_SymLogNorm():
216222
"""
217223
Test SymLogNorm behavior

0 commit comments

Comments
 (0)