colormap-manipulation
Creating Colormaps in Matplotlib
Matplotlib has a number of built-in colormaps accessible viamatplotlib.cm.get_cmap. There are also external libraries like
palettable that have many extra colormaps.
However, we often want to create or manipulate colormaps in Matplotlib.
This can be done using the class ListedColormap and a Nx4 numpy array of
values between 0 and 1 to represent the RGBA values of the colormap. There
is also a LinearSegmentedColormap class that allows colormaps to be
specified with a few anchor points defining segments, and linearly
interpolating between the anchor points.
Getting colormaps and accessing their values
First, getting a named colormap, most of which are listed in
Choosing Colormaps in Matplotlib requires the use ofmatplotlib.cm.get_cmap, which returns amatplotlib.colors.ListedColormap object. The second argument gives
the size of the list of colors used to define the colormap, and below we
use a modest value of 12 so there are not a lot of values to look at.
1 | import numpy as np |
Out:
1 | <matplotlib.colors.ListedColormap object at 0x7f7f3c724390> |
The object viridis is a callable, that when passed a float between
0 and 1 returns an RGBA value from the colormap:
1 | print(viridis(0.56)) |
Out:
1 | (0.119512, 0.607464, 0.540218, 1.0) |
The list of colors that comprise the colormap can be directly accessed using
the colors property,
or it can be accessed indirectly by calling viridis with an array
of values matching the length of the colormap. Note that the returned list
is in the form of an RGBA Nx4 array, where N is the length of the colormap.
1 | print('viridis.colors', viridis.colors) |
Out:
1 | viridis.colors [[0.267004 0.004874 0.329415 1. ] |
The colormap is a lookup table, so “oversampling” the colormap returns
nearest-neighbor interpolation (note the repeated colors in the list below)
1 | print('viridis(np.linspace(0, 1, 15))', viridis(np.linspace(0, 1, 15))) |
Out:
1 | viridis(np.linspace(0, 1, 15)) [[0.267004 0.004874 0.329415 1. ] |
Creating listed colormaps
This is essential the inverse operation of the above where we supply a
Nx4 numpy array with all values between 0 and 1,
to ListedColormap to make a new colormap. This means that
any numpy operations that we can do on a Nx4 array make carpentry of
new colormaps from existing colormaps quite straight forward.
Suppose we want to make the first 25 entries of a 256-length “viridis”
colormap pink for some reason:
1 | viridis = cm.get_cmap('viridis', 256) |

We can easily reduce the dynamic range of a colormap; here we choose the
middle 0.5 of the colormap. However, we need to interpolate from a larger
colormap, otherwise the new colormap will have repeated values.
1 | viridisBig = cm.get_cmap('viridis', 512) |

and we can easily concatenate two colormaps:
1 | top = cm.get_cmap('Oranges_r', 128) |

Of course we need not start from a named colormap, we just need to create
the Nx4 array to pass to ListedColormap. Here we create a
brown colormap that goes to white….
1 | N = 256 |

Creating linear segmented colormaps
LinearSegmentedColormap class specifies colormaps using anchor points
between which RGB(A) values are interpolated.
The format to specify these colormaps allows discontinuities at the anchor
points. Each anchor point is specified as a row in a matrix of the
form [x[i] yleft[i] yright[i]], where x[i] is the anchor, andyleft[i] and yright[i] are the values of the color on either
side of the anchor point.
If there are no discontinuities, then yleft[i]=yright[i]:
1 | cdict = {'red': [[0.0, 0.0, 0.0], |

In order to make a discontinuity at an anchor point, the third column is
different than the second. The matrix for each of “red”, “green”, “blue”,
and optionally “alpha” is set up as:
1 | cdict['red'] = [... |
and for values passed to the colormap between x[i] and x[i+1],
the interpolation is between yright[i] and yleft[i+1].
In the example below there is a discontinuity in red at 0.5. The
interpolation between 0 and 0.5 goes from 0.3 to 1, and between 0.5 and 1
it goes from 0.9 to 1. Note that red[0, 1], and red[2, 2] are both
superfluous to the interpolation because red[0, 1] is the value to the
left of 0, and red[2, 2] is the value to the right of 1.0.
1 | cdict['red'] = [[0.0, 0.0, 0.3], |

References
The use of the following functions, methods, classes and modules is shown
in this example:
1 | import matplotlib |
Total running time of the script: ( 0 minutes 2.220 seconds)




