Loren on the Art of MATLAB

Turn ideas into MATLAB

Colormap Manipulations 14

Posted by Loren Shure,

We use color as a dimension to understand the world. For some datasets, judicious choice of colormaps provides a useful means for gleaning information.


Basic Colormap Manipulation

Let's start with a grayscale image from the Image Processing Toolbox and display it using imshow.

imshow cameraman.tif

and let's take a quick look at the colormap

cm = get(1,'colormap');
legend(get(gca,'children'),{'blue' 'green' 'red'},'location','NorthWest')

or superpose a colorbar on the image plot.

colorbar SouthOutside

It's difficult at first to see much detail about the man's clothing. Using the MATLAB function brighten, we can change the colormap and see more detail in the darker regions.


Notice the extra detail around the buttons and glove when we brighten the colormap more (at the expense of the lighter background).


Now let's see what the individual channels look like.

cm = get(1,'colormap');
legend(get(gca,'children'),{'blue' 'green' 'red'},'location','NorthWest')

Other "Simple" Colormap Manipulations

Colormaps contain are 3-column matrices with double values lying between 0 and 1. So what manipulations can we easily do to create new colormaps from existing ones? Here are some simple ones.

colorbar off

Get another monotonic colormap and see other colormap choices.

help pink
 PINK   Pastel shades of pink color map
    PINK(M) returns an M-by-3 matrix containing a "pink" colormap.
    PINK, by itself, is the same length as the current figure's
    colormap. If no figure exists, MATLAB creates one.
    For example, to reset the colormap of the current figure:

This is not quite the sepia tones for an old-fashioned photograph look, but it's getting close.



You can run into some pitfalls with colormaps. Here are a few to watch out for.

  • Using the wrong length colormap can lead to very poor results. This makes the display look totally wrong. Try this.
load clown

Now make the colormap gray


Next display the clown using an inappropriate colormap.

  • Colormap is not double or not in the correct range (between 0 and 1).
  • In MATLAB version 4, the default colormap was hsv. The hsv colormap starts and ends with red, making it is very hard to interpret colors as high or low.
axis off

Starting in MATLAB 5, the default colormap is jet.



Here are some resources for learning more about color and colormaps in MATLAB.

What additional resources on color would be useful? Let me know here.

Get the MATLAB code

Published with MATLAB® 7.3


Comments are closed.

14 CommentsOldest to Newest

Ian replied on : 2 of 14

What if you wanted to call the class method ‘get’ from another class? I have a number of different get methods (all stored in their particular @class_name folders), so I assumed that just passing the object would return the correct method ‘get’ (for the specific object). Maybe not?

Jody Klymak replied on : 3 of 14

Colormap are very important. It would be great to see better support for perceptual colormaps.

It would also be nice if more than one colormap could be used per figure. There is a fileexchange entry that does this quite well, but is resource intensive.

Thanks, Jody

Loren replied on : 5 of 14


Your request for multiple colormaps per figure is in our enhancement database and will be incorporated into a future release (not sure which one yet since that’s only a piece of what the team is working on).


Wang replied on : 6 of 14

Hey Loren:
Do you know how to construct a sparse symbolic matrix, it seems sparse command doesn’t support Syms Data

Loren replied on : 7 of 14


sparse only works on certain numeric types. Can you describe your use case for needing sparse with symbolic variables?


Wang replied on : 8 of 14

Hey Loren:
I just need to estimate the blur kernel of the images, as you know, if the image is m-by-n matrix, it’s blur matrix is m-by-n by m-by-n sparse matrix. If we don’t know the value of the blur kernel, we can only represent it as the symbolic format. Thank you very much.

Eric Dick replied on : 9 of 14

Hi Loren,

I’m interested in colormaps with respect to the imshow function. What are the limitations, if any, on the number of grayscale shades that can be displayed with imshow (ie. is it limited to 255 bins from 0 to 1) ?

Thank You,


Brendan Hannigan replied on : 10 of 14

Hi Eric,

The values of MATLAB colormaps must be in the range [0..1]. You can see if you try to set one otherwise you will get an error:

f = figure(‘colormap’,[1.1 1.2 1])
??? Error using ==> figure
All colormap intensities must be between 0 and 1

As for the number of rows allowed in a grayscale colormap, it depends on the figure renderer. By default, figures displaying images use the ‘Painters’ renderer. This renderer has the limitation that it cannot correctly display images with colormaps with more than 256 entries in them. You can see this in the following example:

size = 300;
im = repmat(1:size,size,1);
him = image(im,’CDataMapping’,’direct’);
colorbar southOutside
>> get(gcf,’Renderer’)
ans =

Here you can see that the entries at the end of the colormap are not rendering correctly. This limitation may be removed in a future version of MATLAB.

One solution is to change the figure’s renderer property to either ‘zbuffer’ or ‘opengl’, like this:

size = 300;
im = repmat(1:size,size,1);
him = image(im,’CDataMapping’,’direct’);
colorbar southOutside

Now we see that the gradient goes smoothly all the way to the edge of the image, and the colormap entries beyond #256 are rendering correctly.

If you are displaying indexed images using the IMSHOW function, we check for this issue and change the renderer to ‘zbuffer’ automatically when trying to display indexed images with long colormaps, like this:

size = 300;
im = repmat(1:size,size,1);
him = imshow(im,gray(size));
colorbar southOutside
ans =

hope this helps!

Alex Matyushov replied on : 13 of 14

Hi Lauren,

Why did you say this: “Colormap is not double or not in the correct range (between 0 and 1).”?

I loaded up the clown sample pic just like you did, and then I did:
>> figure, image(X), colormap(gray(256))
>> cmap = get(gcf,’colormap’);

The resulting image looks just like yours that you posted here. But the cmap variable is indeed of type double, and it does range from 0 to 1.


Loren replied on : 14 of 14


What I meant is that the colors weren’t mapped to the full gray scale since X, the clown, only has values from 1 to 81, not 1 to 256.