Note
Go to the endto download the full example code.
Matplotlib has a number of built-in colormaps accessible viamatplotlib.colormaps. There are also external libraries likepalettable that have many extra colormaps.
However, we may also want to create or manipulate our own colormaps.This can be done using the class ListedColormap orLinearSegmentedColormap.Both colormap classes map values between 0 and 1 to colors. There are howeverdifferences, as explained below.
Before manually creating or manipulating colormaps, let us first see how wecan obtain colormaps and their colors from existing colormap classes.
Getting colormaps and accessing their values#
First, getting a named colormap, most of which are listed inChoosing Colormaps in Matplotlib, may be done using matplotlib.colormaps,which returns a colormap object. The length of the list of colors usedinternally to define the colormap can be adjusted via Colormap.resampled.Below we use a modest value of 8 so there are not a lot of values to look at.
import matplotlib.pyplot as pltimport numpy as npimport matplotlib as mplfrom matplotlib.colors import LinearSegmentedColormap, ListedColormapviridis = mpl.colormaps['viridis'].resampled(8)
The object viridis
is a callable, that when passed a float between0 and 1 returns an RGBA value from the colormap:
print(viridis(0.56))
(0.122312, 0.633153, 0.530398, 1.0)
ListedColormap#
ListedColormaps store their color values in a .colors
attribute.The list of colors that comprise the colormap can be directly accessed usingthe colors
property,or it can be accessed indirectly by calling viridis
with an array ofvalues matching the length of the colormap. Note that the returned list isin the form of an RGBA (N, 4) array, where N is the length of the colormap.
print('viridis.colors', viridis.colors)print('viridis(range(8))', viridis(range(8)))print('viridis(np.linspace(0, 1, 8))', viridis(np.linspace(0, 1, 8)))
viridis.colors [[0.267004 0.004874 0.329415 1. ] [0.275191 0.194905 0.496005 1. ] [0.212395 0.359683 0.55171 1. ] [0.153364 0.497 0.557724 1. ] [0.122312 0.633153 0.530398 1. ] [0.288921 0.758394 0.428426 1. ] [0.626579 0.854645 0.223353 1. ] [0.993248 0.906157 0.143936 1. ]]viridis(range(8)) [[0.267004 0.004874 0.329415 1. ] [0.275191 0.194905 0.496005 1. ] [0.212395 0.359683 0.55171 1. ] [0.153364 0.497 0.557724 1. ] [0.122312 0.633153 0.530398 1. ] [0.288921 0.758394 0.428426 1. ] [0.626579 0.854645 0.223353 1. ] [0.993248 0.906157 0.143936 1. ]]viridis(np.linspace(0, 1, 8)) [[0.267004 0.004874 0.329415 1. ] [0.275191 0.194905 0.496005 1. ] [0.212395 0.359683 0.55171 1. ] [0.153364 0.497 0.557724 1. ] [0.122312 0.633153 0.530398 1. ] [0.288921 0.758394 0.428426 1. ] [0.626579 0.854645 0.223353 1. ] [0.993248 0.906157 0.143936 1. ]]
The colormap is a lookup table, so "oversampling" the colormap returnsnearest-neighbor interpolation (note the repeated colors in the list below)
print('viridis(np.linspace(0, 1, 12))', viridis(np.linspace(0, 1, 12)))
viridis(np.linspace(0, 1, 12)) [[0.267004 0.004874 0.329415 1. ] [0.267004 0.004874 0.329415 1. ] [0.275191 0.194905 0.496005 1. ] [0.212395 0.359683 0.55171 1. ] [0.212395 0.359683 0.55171 1. ] [0.153364 0.497 0.557724 1. ] [0.122312 0.633153 0.530398 1. ] [0.288921 0.758394 0.428426 1. ] [0.288921 0.758394 0.428426 1. ] [0.626579 0.854645 0.223353 1. ] [0.993248 0.906157 0.143936 1. ] [0.993248 0.906157 0.143936 1. ]]
LinearSegmentedColormap#
LinearSegmentedColormaps do not have a .colors
attribute.However, one may still call the colormap with an integer array, or with afloat array between 0 and 1.
copper = mpl.colormaps['copper'].resampled(8)print('copper(range(8))', copper(range(8)))print('copper(np.linspace(0, 1, 8))', copper(np.linspace(0, 1, 8)))
copper(range(8)) [[0. 0. 0. 1. ] [0.17647055 0.1116 0.07107143 1. ] [0.35294109 0.2232 0.14214286 1. ] [0.52941164 0.3348 0.21321429 1. ] [0.70588219 0.4464 0.28428571 1. ] [0.88235273 0.558 0.35535714 1. ] [1. 0.6696 0.42642857 1. ] [1. 0.7812 0.4975 1. ]]copper(np.linspace(0, 1, 8)) [[0. 0. 0. 1. ] [0.17647055 0.1116 0.07107143 1. ] [0.35294109 0.2232 0.14214286 1. ] [0.52941164 0.3348 0.21321429 1. ] [0.70588219 0.4464 0.28428571 1. ] [0.88235273 0.558 0.35535714 1. ] [1. 0.6696 0.42642857 1. ] [1. 0.7812 0.4975 1. ]]
Creating listed colormaps#
Creating a colormap is essentially the inverse operation of the above wherewe supply a list or array of color specifications to ListedColormap tomake a new colormap.
Before continuing with the tutorial, let us define a helper function thattakes one of more colormaps as input, creates some random data and appliesthe colormap(s) to an image plot of that dataset.
def plot_examples(colormaps): """ Helper function to plot data with associated colormap. """ np.random.seed(19680801) data = np.random.randn(30, 30) n = len(colormaps) fig, axs = plt.subplots(1, n, figsize=(n * 2 + 2, 3), layout='constrained', squeeze=False) for [ax, cmap] in zip(axs.flat, colormaps): psm = ax.pcolormesh(data, cmap=cmap, rasterized=True, vmin=-4, vmax=4) fig.colorbar(psm, ax=ax) plt.show()
In the simplest case we might type in a list of color names to create acolormap from those.
cmap = ListedColormap(["darkorange", "gold", "lawngreen", "lightseagreen"])plot_examples([cmap])
In fact, that list may contain any validMatplotlib color specification.Particularly useful for creating custom colormaps are (N, 4)-shaped arrays.Because with the variety of numpy operations that we can do on a such anarray, carpentry of new colormaps from existing colormaps become quitestraight forward.
For example, suppose we want to make the first 25 entries of a 256-length"viridis" colormap pink for some reason:
We can reduce the dynamic range of a colormap; here we choose themiddle half of the colormap. Note, however, that because viridis is alisted colormap, we will end up with 128 discrete values instead of the 256values that were in the original colormap. This method does not interpolatein color-space to add new colors.
viridis_big = mpl.colormaps['viridis']newcmp = ListedColormap(viridis_big(np.linspace(0.25, 0.75, 128)))plot_examples([viridis, newcmp])
and we can easily concatenate two colormaps:
top = mpl.colormaps['Oranges_r'].resampled(128)bottom = mpl.colormaps['Blues'].resampled(128)newcolors = np.vstack((top(np.linspace(0, 1, 128)), bottom(np.linspace(0, 1, 128))))newcmp = ListedColormap(newcolors, name='OrangeBlue')plot_examples([viridis, newcmp])
Of course we need not start from a named colormap, we just need to createthe (N, 4) array to pass to ListedColormap. Here we create a colormap thatgoes from brown (RGB: 90, 40, 40) to white (RGB: 255, 255, 255).
N = 256vals = np.ones((N, 4))vals[:, 0] = np.linspace(90/256, 1, N)vals[:, 1] = np.linspace(40/256, 1, N)vals[:, 2] = np.linspace(40/256, 1, N)newcmp = ListedColormap(vals)plot_examples([viridis, newcmp])
Creating linear segmented colormaps#
The LinearSegmentedColormap class specifies colormaps using anchor pointsbetween which RGB(A) values are interpolated.
The format to specify these colormaps allows discontinuities at the anchorpoints. Each anchor point is specified as a row in a matrix of theform [x[i] yleft[i] yright[i]]
, where x[i]
is the anchor, andyleft[i]
and yright[i]
are the values of the color on eitherside of the anchor point.
If there are no discontinuities, then yleft[i] == yright[i]
:
cdict = {'red': [[0.0, 0.0, 0.0], [0.5, 1.0, 1.0], [1.0, 1.0, 1.0]], 'green': [[0.0, 0.0, 0.0], [0.25, 0.0, 0.0], [0.75, 1.0, 1.0], [1.0, 1.0, 1.0]], 'blue': [[0.0, 0.0, 0.0], [0.5, 0.0, 0.0], [1.0, 1.0, 1.0]]}def plot_linearmap(cdict): newcmp = LinearSegmentedColormap('testCmap', segmentdata=cdict, N=256) rgba = newcmp(np.linspace(0, 1, 256)) fig, ax = plt.subplots(figsize=(4, 3), layout='constrained') col = ['r', 'g', 'b'] for xx in [0.25, 0.5, 0.75]: ax.axvline(xx, color='0.7', linestyle='--') for i in range(3): ax.plot(np.arange(256)/256, rgba[:, i], color=col[i]) ax.set_xlabel('index') ax.set_ylabel('RGB') plt.show()plot_linearmap(cdict)
In order to make a discontinuity at an anchor point, the third column isdifferent than the second. The matrix for each of "red", "green", "blue",and optionally "alpha" is set up as:
cdict['red'] = [... [x[i] yleft[i] yright[i]], [x[i+1] yleft[i+1] yright[i+1]], ...]
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. Theinterpolation between 0 and 0.5 goes from 0.3 to 1, and between 0.5 and 1it goes from 0.9 to 1. Note that red[0, 1]
, and red[2, 2]
are bothsuperfluous to the interpolation because red[0, 1]
(i.e., yleft[0]
)is the value to the left of 0, and red[2, 2]
(i.e., yright[2]
) is thevalue to the right of 1, which are outside the color mapping domain.
Directly creating a segmented colormap from a list#
The approach described above is very versatile, but admittedly a bitcumbersome to implement. For some basic cases, the use ofLinearSegmentedColormap.from_list may be easier. This creates a segmentedcolormap with equal spacings from a supplied list of colors.
If desired, the nodes of the colormap can be given as numbers between 0 and1. For example, one could have the reddish part take more space in thecolormap.
Reversing a colormap#
Colormap.reversed creates a new colormap that is a reversed version ofthe original colormap.
If no name is passed in, .reversed
also names the copy byappending '_r' to the original colormap'sname.
Registering a colormap#
Colormaps can be added to the matplotlib.colormaps list of named colormaps.This allows the colormaps to be accessed by name in plotting functions:
References
The use of the following functions, methods, classes and modules is shownin this example:
matplotlib.axes.Axes.pcolormesh
matplotlib.figure.Figure.colorbar
matplotlib.colors
matplotlib.colors.LinearSegmentedColormap
matplotlib.colors.ListedColormap
matplotlib.cm
matplotlib.colormaps
Total running time of the script: (0 minutes 5.601 seconds)
Download Jupyter notebook: colormap-manipulation.ipynb
Download Python source code: colormap-manipulation.py