1
1
.. _colormapnorm-tutorial :
2
2
3
- Colormap Normaliztions
4
- ================================
3
+ Colormap Normalization
4
+ ======================
5
5
6
6
Objects that use colormaps by default linearly map the colors in the
7
7
colormap from data values *vmin * to *vmax *. For example::
@@ -13,21 +13,19 @@ give a color at the center of the colormap *RdBu_r* (white in this
13
13
case).
14
14
15
15
Matplotlib does this mapping in two steps, with a normalization from
16
- [0,1] occuring first, and then mapping onto the indices in the
17
- colormap. Normalizations are defined as part of
18
- :func: `matplotlib.colors ` module. The default normalization is
16
+ [0,1] occurring first, and then mapping onto the indices in the
17
+ colormap. Normalizations are classes defined in the
18
+ :func: `matplotlib.colors ` module. The default, linear normalization is
19
19
:func: `matplotlib.colors.Normalize `.
20
20
21
- The artists that map data to
22
- color pass the arguments *vmin * and *vmax * to
23
- :func: `matplotlib.colors.Normalize `. We can substnatiate the
24
- normalization and see what it returns. In this case it returns 0.5:
21
+ Artists that map data to color pass the arguments *vmin * and *vmax * to
22
+ construct a :func: `matplotlib.colors.Normalize ` instance, then call it:
25
23
26
24
.. ipython ::
27
25
28
26
In [1]: import matplotlib as mpl
29
27
30
- In [2]: norm= mpl.colors.Normalize(vmin=-1.,vmax=1.)
28
+ In [2]: norm = mpl.colors.Normalize(vmin=-1.,vmax=1.)
31
29
32
30
In [3]: norm(0.)
33
31
Out[3]: 0.5
@@ -36,69 +34,69 @@ However, there are sometimes cases where it is useful to map data to
36
34
colormaps in a non-linear fashion.
37
35
38
36
Logarithmic
39
- ---------------------------------
37
+ -----------
40
38
41
- One of the most common transformations is to plot data by taking its
42
- logarithm (to the base-10). This transformation is useful when there
43
- are changes across disparate scales that we still want to be able to
44
- see. Using :func: ` colors.LogNorm ` normalizes the data by
45
- :math: `log_{ 10 }`. In the example below, there are two bumps, one much
46
- smaller than the other. Using :func: `colors.LogNorm `, the shape and
47
- location of each bump can clearly be seen:
39
+ One of the most common transformations is to plot data by taking
40
+ its logarithm (to the base-10). This transformation is useful to
41
+ display changes across disparate scales. Using :func: ` colors.LogNorm `
42
+ normalizes the data via :math: `log_{ 10 }`. In the example below,
43
+ there are two bumps, one much smaller than the other. Using
44
+ :func: `colors.LogNorm `, the shape and location of each bump can clearly
45
+ be seen:
48
46
49
47
.. plot :: users/plotting/examples/colormap_normalizations_lognorm.py
50
48
:include-source:
51
49
52
- Symetric logarithmic
53
- ---------------------------------
50
+ Symmetric logarithmic
51
+ ---------------------
54
52
55
53
Similarly, it sometimes happens that there is data that is positive
56
54
and negative, but we would still like a logarithmic scaling applied to
57
55
both. In this case, the negative numbers are also scaled
58
- logarithmically, and mapped to small numbers. i.e. If `vmin=-vmax `,
56
+ logarithmically, and mapped to smaller numbers; e.g., if `vmin=-vmax `,
59
57
then they the negative numbers are mapped from 0 to 0.5 and the
60
58
positive from 0.5 to 1.
61
59
62
- Since the values close to zero tend toward infinity, there is a need
63
- to have a range around zero that is linear . The parameter * linthresh *
64
- allows the user to specify the size of this range (-* linthresh *,
65
- *linthresh *). The size of this range in the colormap is set by
66
- *linscale *. When *linscale * == 1.0 (the default), the space used for
67
- the positive and negative halves of the linear range will be equal to
68
- one decade in the logarithmic range.
60
+ Since the logarithm of values close to zero tends toward infinity, a
61
+ small range around zero needs to be mapped linearly . The parameter
62
+ * linthresh * allows the user to specify the size of this range
63
+ (- *linthresh *, * linthresh * ). The size of this range in the colormap is
64
+ set by *linscale *. When *linscale * == 1.0 (the default), the space used
65
+ for the positive and negative halves of the linear range will be equal
66
+ to one decade in the logarithmic range.
69
67
70
68
.. plot :: users/plotting/examples/colormap_normalizations_symlognorm.py
71
69
:include-source:
72
70
73
71
Power-law
74
- ---------------------------------
72
+ ---------
75
73
76
74
Sometimes it is useful to remap the colors onto a power-law
77
75
relationship (i.e. :math: `y=x^{\gamma }`, where :math: `\gamma ` is the
78
76
power). For this we use the :func: `colors.PowerNorm `. It takes as an
79
- argument *gamma * ( *gamma * == 1.0 will just yield the defalut linear
77
+ argument *gamma * (*gamma * == 1.0 will just yield the default linear
80
78
normalization):
81
79
82
80
.. note ::
83
81
84
82
There should probably be a good reason for plotting the data using
85
83
this type of transformation. Technical viewers are used to linear
86
84
and logarithmic axes and data transformations. Power laws are less
87
- common, and viewers should explictly be made aware that they have
85
+ common, and viewers should explicitly be made aware that they have
88
86
been used.
89
87
90
88
91
89
.. plot :: users/plotting/examples/colormap_normalizations_power.py
92
90
:include-source:
93
91
94
92
Discrete bounds
95
- ---------------------------------
93
+ ---------------
96
94
97
95
Another normaization that comes with matplolib is
98
96
:func: `colors.BoundaryNorm `. In addition to *vmin * and *vmax *, this
99
97
takes as arguments boundaries between which data is to be mapped. The
100
98
colors are then linearly distributed between these "bounds". For
101
- instance, if :
99
+ instance:
102
100
103
101
.. ipython ::
104
102
@@ -118,20 +116,19 @@ Note unlike the other norms, this norm returns values from 0 to *ncolors*-1.
118
116
119
117
120
118
Custom normalization: Two linear ranges
121
- -----------------------------------------
119
+ ---------------------------------------
122
120
123
- It is possible to define your own normalization. This example
124
- plots the same data as the :func: `colors:SymLogNorm ` example, but
125
- a different linear map is used for the negative data values than
126
- the positive. (Note that this example is simple, and does not account
127
- for the edge cases like masked data or invalid values of *vmin * and
128
- *vmax *)
121
+ It is possible to define your own normalization. In the following
122
+ example, we modify :func: `colors:SymLogNorm ` to use different linear
123
+ maps for the negative data values and the positive. (Note that this
124
+ example is simple, and does not validate inputs or account for complex
125
+ cases such as masked data)
129
126
130
127
.. note ::
131
- This may appear soon as :func: `colors.OffsetNorm `
128
+ This may appear soon as :func: `colors.OffsetNorm `.
132
129
133
- As above, non-symetric mapping of data to color is non-standard
134
- practice for quantitative data, and should only be used advisedly. A
130
+ As above, non-symmetric mapping of data to color is non-standard
131
+ practice for quantitative data, and should only be used advisedly. A
135
132
practical example is having an ocean/land colormap where the land and
136
133
ocean data span different ranges.
137
134
0 commit comments