Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The method to obtain the result of a color from a LinearSegmentedColormap instance is to use the method get_color(). This method takes a scalar value from 0 to 1 and returns the corresponding RGB color value from the colormap.

For example, if cmap is an instance of LinearSegmentedColormap with a range of colors, and value is a scalar value between 0 and 1, the following code can be used to obtain the color value at that given scalar value:

import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap # create a colormap cmap = LinearSegmentedColormap.from_list('mycmap', ['red', 'green', 'blue']) # get the color at a scalar value value = 0.5 color = cmap.get_color(value) # plot a colorbar with the colormap fig, ax = plt.subplots() cbar = plt.colorbar(plt.cm.ScalarMappable(norm=None, cmap=cmap)) cbar.set_ticks([0, 0.5, 1]) cbar.set_ticklabels(['0', '0.5', '1']) # show the color value print('The color at scalar value', value, 'is', color) 

This will output the color value at the scalar value of 0.5 in the following format:

The color at scalar value 0.5 is [ 0.5  0.5  1. ]