Chris Slocum

Colorado State University


Python - Custom Colormaps for Matplotlib

Return to the Resources page.

Update 7 March 2018: A more Pythonic version of the function below is available on GitHub under a BSD 3-clause license at https://github.com/CSlocumWX/custom_colormap. Only the text part of this page is CC BY-NC-ND 3.0.

I wrote the following Python function to generate custom colormaps for Matplotlib as the TA for ATS 607 in 2013. The function allows you to create a list of tuples with 8-bit (0 to 255) or arithmetic (0.0 to 1.0) RGB values to create linear colormaps.

I made modifications to the code to be able to generate non-uniform colormaps. Below is a section of a full disk water vapor image from GOES-11 from Oct 2011 which shows an extreme example (256 seperate RBG values non-uniformly spaced) of how to use the code below.

GOES-11 (GOES-WEST) water vapor imagery from Oct 2011. A special thanks to
Dan Lindsey of NOAA/NESDIS/STAR/RAMM Branch for providing the color table used
in CIRA's water vapor products.

Please feel free to contact me with any feedback, questions, comments, or concerns. My contact information can be found on my about page.

Please note that this tutorial for Python - Custom Colormaps for Matplotlib by Chris Slocum is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. The code is under a BSD 3-clause license and is available at https://github.com/CSlocumWX/custom_colormap.

Example output

Source Code

'''
NAME
    Custom Colormaps for Matplotlib
PURPOSE
    This program shows how to implement make_cmap which is a function that
    generates a colorbar.  If you want to look at different color schemes,
    check out https://kuler.adobe.com/create.
PROGRAMMER(S)
    Chris Slocum
REVISION HISTORY
    20130411 -- Initial version created
    20140313 -- Small changes made and code posted online
    20140320 -- Added the ability to set the position of each color
'''

def make_cmap(colors, position=None, bit=False):
    '''
    make_cmap takes a list of tuples which contain RGB values. The RGB
    values may either be in 8-bit [0 to 255] (in which bit must be set to
    True when called) or arithmetic [0 to 1] (default). make_cmap returns
    a cmap with equally spaced colors.
    Arrange your tuples so that the first color is the lowest value for the
    colorbar and the last is the highest.
    position contains values from 0 to 1 to dictate the location of each color.
    '''
    import matplotlib as mpl
    import numpy as np
    bit_rgb = np.linspace(0,1,256)
    if position == None:
        position = np.linspace(0,1,len(colors))
    else:
        if len(position) != len(colors):
            sys.exit("position length must be the same as colors")
        elif position[0] != 0 or position[-1] != 1:
            sys.exit("position must start with 0 and end with 1")
    if bit:
        for i in range(len(colors)):
            colors[i] = (bit_rgb[colors[i][0]],
                         bit_rgb[colors[i][1]],
                         bit_rgb[colors[i][2]])
    cdict = {'red':[], 'green':[], 'blue':[]}
    for pos, color in zip(position, colors):
        cdict['red'].append((pos, color[0], color[0]))
        cdict['green'].append((pos, color[1], color[1]))
        cdict['blue'].append((pos, color[2], color[2]))

    cmap = mpl.colors.LinearSegmentedColormap('my_colormap',cdict,256)
    return cmap

### An example of how to use make_cmap
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(311)
### Create a list of RGB tuples
colors = [(255,0,0), (255,255,0), (255,255,255), (0,157,0), (0,0,255)] # This example uses the 8-bit RGB
### Call the function make_cmap which returns your colormap
my_cmap = make_cmap(colors, bit=True)
### Use your colormap
plt.pcolor(np.random.rand(25,50), cmap=my_cmap)
plt.colorbar()

ax = fig.add_subplot(312)
colors = [(1,1,1), (0.5,0,0)] # This example uses the arithmetic RGB
### If you are only going to use your colormap once you can
### take out a step.
plt.pcolor(np.random.rand(25,50), cmap=make_cmap(colors))
plt.colorbar()

ax = fig.add_subplot(313)
colors = [(0.4,0.2,0.0), (1,1,1), (0,0.3,0.4)]
### Create an array or list of positions from 0 to 1.
position = [0, 0.3, 1]
plt.pcolor(np.random.rand(25,50), cmap=make_cmap(colors, position=position))
plt.colorbar()

plt.savefig("custom_cmap.png")
plt.show()

Downloads

Return to the top of the page.


Creative Commons License
Python - Custom Colormaps for Matplotlib by Chris Slocum is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.