1
votes

I'm trying to understand how matplotlib colormaps work. Consider the following code:

import matplotlib.pyplot as plt
cmap = plt.get_cmap('jet')
print cmap(200)

which prints

(1.0, 0.46550472040668145, 0.0, 1.0)

So my understanding is that a colormap maps a numerical value (200 in this case) to a color value (1.0, 0.46550472040668145, 0.0, 1.0 in this case). How does matplotlib set the range for its colormap?

Is it possible to define a maximum and a minimum value between which a linear map is applied? With imshow() one can set a vmin and a vmax parameter, however, I would have to do it at the colormap level because I'm providing the colormap to another function later on.

This might be a more general question on how to colormaps work; in seaborn's color palettes, for example, there is no option for the range either.

1

1 Answers

4
votes

The range of colormaps is always between 0 and 1. You will need to normalize your data to this range. For example, to map the range between 0 and 400 linearly to the colors of a colormap,

import matplotlib.pyplot as plt
cmap = plt.get_cmap('viridis')
norm = plt.Normalize(0, 400)

color = cmap(norm(200.))