colormapnorms
Colormap Normalization
Objects that use colormaps by default linearly map the colors in the
colormap from data values vmin to vmax. For example:
1 | pcm = ax.pcolormesh(x, y, Z, vmin=-1., vmax=1., cmap='RdBu_r') |
will map the data in Z linearly from -1 to +1, so Z=0 will
give a color at the center of the colormap RdBu_r (white in this
case).
Matplotlib does this mapping in two steps, with a normalization from
the input data to [0, 1] occurring first, and then mapping onto the
indices in the colormap. Normalizations are classes defined in thematplotlib.colors() module. The default, linear normalization
is matplotlib.colors.Normalize().
Artists that map data to color pass the arguments vmin and vmax to
construct a matplotlib.colors.Normalize() instance, then call it:
1 | In [1]: import matplotlib as mpl |
However, there are sometimes cases where it is useful to map data to
colormaps in a non-linear fashion.
Logarithmic
One of the most common transformations is to plot data by taking its logarithm
(to the base-10). This transformation is useful to display changes across
disparate scales. Using colors.LogNorm normalizes the data via
(log_{10}). In the example below, there are two bumps, one much smaller
than the other. Using colors.LogNorm, the shape and location of each bump
can clearly be seen:
1 | import numpy as np |

Symmetric logarithmic
Similarly, it sometimes happens that there is data that is positive
and negative, but we would still like a logarithmic scaling applied to
both. In this case, the negative numbers are also scaled
logarithmically, and mapped to smaller numbers; e.g., if vmin=-vmax,
then they the negative numbers are mapped from 0 to 0.5 and the
positive from 0.5 to 1.
Since the logarithm of values close to zero tends toward infinity, a
small range around zero needs to be mapped linearly. The parameter
linthresh allows the user to specify the size of this range
(-linthresh, linthresh). The size of this range in the colormap is
set by linscale. When linscale == 1.0 (the default), the space used
for the positive and negative halves of the linear range will be equal
to one decade in the logarithmic range.
1 | N = 100 |

Power-law
Sometimes it is useful to remap the colors onto a power-law
relationship (i.e. (y=x^{\gamma}), where (\gamma) is the
power). For this we use the colors.PowerNorm(). It takes as an
argument gamma (gamma == 1.0 will just yield the default linear
normalization):
::: tip Note
There should probably be a good reason for plotting the data using
this type of transformation. Technical viewers are used to linear
and logarithmic axes and data transformations. Power laws are less
common, and viewers should explicitly be made aware that they have
been used.
:::
1 | N = 100 |

Discrete bounds
Another normaization that comes with Matplotlib iscolors.BoundaryNorm(). In addition to vmin and vmax, this
takes as arguments boundaries between which data is to be mapped. The
colors are then linearly distributed between these “bounds”. For
instance:
1 | In [4]: import matplotlib.colors as colors |
Note unlike the other norms, this norm returns values from 0 to ncolors-1.
1 | N = 100 |

DivergingNorm: Different mapping on either side of a center
Sometimes we want to have a different colormap on either side of a
conceptual center point, and we want those two colormaps to have
different linear scales. An example is a topographic map where the land
and ocean have a center at zero, but land typically has a greater
elevation range than the water has depth range, and they are often
represented by a different colormap.
1 | filename = cbook.get_sample_data('topobathy.npz', asfileobj=False) |

Custom normalization: Manually implement two linear ranges
The DivergingNorm described above makes a useful example for
defining your own norm.
1 | class MidpointNormalize(colors.Normalize): |

Total running time of the script: ( 0 minutes 1.895 seconds)




