About matplotlib colormap and how to get RGB values of the map

matplotlib comes with lots of colormaps. First, let us see where and how to find them. Then, we will see how to extract individual colors (e.g., in RGB) from the colormap itself.

Let us first creat an image

By default, the jet colormap is used. You should see something like this

post_sept_2014_1

You can chnage the colormap easily but you need to know the name of the colormap first. Some colormaps are available as functions. For instance the copper one:

post_sept_2014_2

How can we know the names of the colormap ? Check the documentation of colormaps itself for details or to obtain the full list of colormap names, type

as mentionned above, some colormaps are available as functions. If not, you should use the function set_cmap:

Finally, if you want to extract the color contained in a colormap, use the cm function:

colormaps are usually encoded with N=256 colors. To figure out the first one, we use the code as above. If you want the latest one, use cm.jet(255)
Note that the returned objet is tuple of 4 items. The last one being the transparency.

This entry was posted in Python and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published.