I often have to create plots where the color of some element reflects an
underlying value. This mapping of color -> value is generally easily
accomplished by using colormaps in matplotlib. The standard provided colormaps
(such as cm.jet or cm.cool) map values over the interval [0,1]. Often times,
however, our data will have a different range and will need to be normalized in
order for it to be mapped to the full range of available colors.
The example below illustrates what the color range looks like without the
normalization (top two plots) and with the normalization (bottom plot).
Notice how the unnormalized [-1,1] input values have a dark blue color over the
left half of the second plot. This is because cm.jet(-1) = cm.jet(-.5) =
cm.jet(0.) Values outside of cm.jet’s accepted input range of [0,1] all
return the same color. By using the Normalize class, the input values are
scaled to the [0,1] input range and the output colors span the entire range of
the colormap (third plot).