11.. _colormapnorm-tutorial :
22
3- Colormap Normaliztions
3+ Colormap Normaliztions
44================================
55
66Objects that use colormaps by default linearly map the colors in the
@@ -10,34 +10,36 @@ colormap from data values *vmin* to *vmax*. For example::
1010
1111will map the data in *Z * linearly from -1 to +1, so *Z=0 * will
1212give a color at the center of the colormap *RdBu_r * (white in this
13- case).
13+ case).
1414
1515Matplotlib does this mapping in two steps, with a normalization from
1616[0,1] occuring first, and then mapping onto the indices in the
1717colormap. Normalizations are defined as part of
1818:func: `matplotlib.colors ` module. The default normalization is
19- :func: `matplotlib.colors.Normalize `. The artists that map data to
19+ :func: `matplotlib.colors.Normalize `.
20+
21+ The artists that map data to
2022color pass the arguments *vmin * and *vmax * to
2123:func: `matplotlib.colors.Normalize `. We can substnatiate the
2224normalization and see what it returns. In this case it returns 0.5:
2325
2426.. ipython ::
2527
2628 In [1]: import matplotlib as mpl
27-
29+
2830 In [2]: norm=mpl.colors.Normalize(vmin=-1.,vmax=1.)
29-
31+
3032 In [3]: norm(0.)
3133 Out[3]: 0.5
3234
3335However, there are sometimes cases where it is useful to map data to
34- colormaps in a non-linear fashion.
36+ colormaps in a non-linear fashion.
3537
3638Logarithmic
3739---------------------------------
3840
3941One of the most common transformations is to plot data by taking its
40- logarithm (to the base-10). This transofrmation is useful when there
42+ logarithm (to the base-10). This transformation is useful when there
4143are changes across disparate scales that we still want to be able to
4244see. Using :func: `colors.LogNorm ` normalizes the data by
4345:math: `log_{10 }`. In the example below, there are two bumps, one much
@@ -78,7 +80,7 @@ argument *gamma* ( *gamma* == 1.0 will just yield the defalut linear
7880normalization):
7981
8082.. note ::
81-
83+
8284 There should probably be a good reason for plotting the data using
8385 this type of transformation. Technical viewers are used to linear
8486 and logarithmic axes and data transformations. Power laws are less
@@ -89,6 +91,31 @@ normalization):
8991.. plot :: users/plotting/examples/colormap_normalizations_power.py
9092 :include-source:
9193
94+ Discrete bounds
95+ ---------------------------------
96+
97+ Another normaization that comes with matplolib is
98+ :func: `colors.BoundaryNorm `. In addition to *vmin * and *vmax *, this
99+ takes as arguments boundaries between which data is to be mapped. The
100+ colors are then linearly distributed between these "bounds". For
101+ instance, if:
102+
103+ .. ipython ::
104+
105+ In [2]: import matplotlib.colors as colors
106+
107+ In [3]: bounds = np.array([-0.25, -0.125, 0, 0.5, 1])
108+
109+ In [4]: norm = colors.BoundaryNorm(boundaries=bounds, ncolors=4)
110+
111+ In [5]: print norm([-0.2,-0.15,-0.02, 0.3, 0.8, 0.99])
112+ [0 0 1 2 3 3]
113+
114+ Note unlike the other norms, this norm returns values from 0 to *ncolors *-1.
115+
116+ .. plot :: users/plotting/examples/colormap_normalizations_bounds.py
117+ :include-source:
118+
92119
93120Custom normalization: Two linear ranges
94121-----------------------------------------
@@ -104,29 +131,9 @@ for the edge cases like masked data or invalid values of *vmin* and
104131 This may appear soon as :func: `colors.OffsetNorm `
105132
106133 As above, non-symetric mapping of data to color is non-standard
107- practice, and should only be used advisedly.
134+ practice for quantitative data, and should only be used advisedly. A
135+ practical example is having an ocean/land colormap where the land and
136+ ocean data span different ranges.
108137
109138.. plot :: users/plotting/examples/colormap_normalizations_custom.py
110139 :include-source:
111-
112- Discrete bounds
113- ---------------------------------
114-
115- Another normaization that comes with matplolib is
116- :func: `colors.BoundaryNorm `. In addition to *vmin * and *vmax *, this
117- takes as arguments boundaries between which data is to be mapped. The
118- colors are then linearly distributed between these "bounds". For
119- instance, if ::
120-
121- bounds = np.array([-0.25, -0.125, 0, 0.5, 1])
122- norm = colors.BoundaryNorm(boundaries=bounds, ncolors=4)
123- print norm([-0.2,-0.15,-0.02, 0.3, 0.8, 0.99])
124-
125- This returns: [0, 0, 1, 2, 3, 3]. Note unlike the other norms, this
126- norm returns values from 0 to *ncolors *-1.
127-
128-
129- .. plot :: users/plotting/examples/colormap_normalizations_bounds.py
130- :include-source:
131-
132-
0 commit comments