1
1
.. _colormapnorm-tutorial :
2
2
3
- Colormap Normaliztions
3
+ Colormap Normaliztions
4
4
================================
5
5
6
6
Objects that use colormaps by default linearly map the colors in the
@@ -10,34 +10,36 @@ colormap from data values *vmin* to *vmax*. For example::
10
10
11
11
will map the data in *Z * linearly from -1 to +1, so *Z=0 * will
12
12
give a color at the center of the colormap *RdBu_r * (white in this
13
- case).
13
+ case).
14
14
15
15
Matplotlib does this mapping in two steps, with a normalization from
16
16
[0,1] occuring first, and then mapping onto the indices in the
17
17
colormap. Normalizations are defined as part of
18
18
: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
20
22
color pass the arguments *vmin * and *vmax * to
21
23
:func: `matplotlib.colors.Normalize `. We can substnatiate the
22
24
normalization and see what it returns. In this case it returns 0.5:
23
25
24
26
.. ipython ::
25
27
26
28
In [1]: import matplotlib as mpl
27
-
29
+
28
30
In [2]: norm=mpl.colors.Normalize(vmin=-1.,vmax=1.)
29
-
31
+
30
32
In [3]: norm(0.)
31
33
Out[3]: 0.5
32
34
33
35
However, 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.
35
37
36
38
Logarithmic
37
39
---------------------------------
38
40
39
41
One 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
41
43
are changes across disparate scales that we still want to be able to
42
44
see. Using :func: `colors.LogNorm ` normalizes the data by
43
45
: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
78
80
normalization):
79
81
80
82
.. note ::
81
-
83
+
82
84
There should probably be a good reason for plotting the data using
83
85
this type of transformation. Technical viewers are used to linear
84
86
and logarithmic axes and data transformations. Power laws are less
@@ -89,6 +91,31 @@ normalization):
89
91
.. plot :: users/plotting/examples/colormap_normalizations_power.py
90
92
:include-source:
91
93
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
+
92
119
93
120
Custom normalization: Two linear ranges
94
121
-----------------------------------------
@@ -104,29 +131,9 @@ for the edge cases like masked data or invalid values of *vmin* and
104
131
This may appear soon as :func: `colors.OffsetNorm `
105
132
106
133
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.
108
137
109
138
.. plot :: users/plotting/examples/colormap_normalizations_custom.py
110
139
: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