Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (2024)

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])
Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (1)

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:

viridis = mpl.colormaps['viridis'].resampled(256)newcolors = viridis(np.linspace(0, 1, 256))pink = np.array([248/256, 24/256, 148/256, 1])newcolors[:25, :] = pinknewcmp = ListedColormap(newcolors)plot_examples([viridis, newcmp])
Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (2)

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])
Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (3)

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])
Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (4)

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 Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (5)

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)
Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (6)

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.

cdict['red'] = [[0.0, 0.0, 0.3], [0.5, 1.0, 0.9], [1.0, 1.0, 1.0]]plot_linearmap(cdict)
Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (7)

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.

colors = ["darkorange", "gold", "lawngreen", "lightseagreen"]cmap1 = LinearSegmentedColormap.from_list("mycmap", 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.

nodes = [0.0, 0.4, 0.8, 1.0]cmap2 = LinearSegmentedColormap.from_list("mycmap", list(zip(nodes, colors)))plot_examples([cmap1, cmap2])
Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (8)

Reversing a colormap#

Colormap.reversed creates a new colormap that is a reversed version ofthe original colormap.

colors = ["#ffffcc", "#a1dab4", "#41b6c4", "#2c7fb8", "#253494"]my_cmap = ListedColormap(colors, name="my_cmap")my_cmap_r = my_cmap.reversed()plot_examples([my_cmap, my_cmap_r])
Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (9)

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:

# my_cmap, my_cmap_r from reversing a colormapmpl.colormaps.register(cmap=my_cmap)mpl.colormaps.register(cmap=my_cmap_r)data = [[1, 2, 3, 4, 5]]fig, (ax1, ax2) = plt.subplots(nrows=2)ax1.imshow(data, cmap='my_cmap')ax2.imshow(data, cmap='my_cmap_r')plt.show()
Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (10)

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

Gallery generated by Sphinx-Gallery

Creating Colormaps in Matplotlib — Matplotlib 3.9.1 documentation (2024)
Top Articles
Woning Simonszand te huur in Delfzijl
The Best Rental Listing Sites for Home Seekers Today
Pollen Count Los Altos
Overton Funeral Home Waterloo Iowa
Nfr Daysheet
PontiacMadeDDG family: mother, father and siblings
Shorthand: The Write Way to Speed Up Communication
Hay day: Top 6 tips, tricks, and cheats to save cash and grow your farm fast!
Riegler & Partner Holding GmbH auf LinkedIn: Wie schätzen Sie die Entwicklung der Wohnraumschaffung und Bauwirtschaft…
Goteach11
Graveguard Set Bloodborne
Which aspects are important in sales |#1 Prospection
Baseball-Reference Com
General Info for Parents
Lonadine
Aspen.sprout Forum
National Office Liquidators Llc
Rams vs. Lions highlights: Detroit defeats Los Angeles 26-20 in overtime thriller
Huntersville Town Billboards
Scout Shop Massapequa
Melissababy
St Clair County Mi Mugshots
Village
Southwest Flight 238
Mikayla Campinos: Unveiling The Truth Behind The Leaked Content
Anesthesia Simstat Answers
John Philip Sousa Foundation
Skepticalpickle Leak
Paradise Point Animal Hospital With Veterinarians On-The-Go
Dl.high Stakes Sweeps Download
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
Eaccess Kankakee
Wasmo Link Telegram
Bozjan Platinum Coins
Yoshidakins
The Wichita Beacon from Wichita, Kansas
How to Watch the X Trilogy Starring Mia Goth in Chronological Order
Goodwill Thrift Store & Donation Center Marietta Photos
Avance Primary Care Morrisville
Finland’s Satanic Warmaster’s Werwolf Discusses His Projects
Myql Loan Login
Post A Bid Monticello Mn
Costco Gas Foster City
Celsius Claims Agent
Penny Paws San Antonio Photos
Killer Intelligence Center Download
877-552-2666
303-615-0055
Costco Tire Promo Code Michelin 2022
Download Twitter Video (X), Photo, GIF - Twitter Downloader
Texas Lottery Daily 4 Winning Numbers
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 5932

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.