File Exchange Pick of the Week

Using multiple colormaps in a single figure 12

Posted by Brett Shoelson,

Former "Pickmaster" (and current blogger) Doug Hull suggested that John Iversen's color freezer might be "Pickworthy." I wholeheartedly agree.

Suppose you wanted to display the image of a clown that ships as a MATLAB demo. This snippet would do the trick:

load clown;
image(X);
colormap(map);

Similarly, if you wanted to display a penny with a copper colormap, this would work:

load penny.mat
contour(P,15)
colormap(copper)
axis ij square
pcolor(P)
axis ij square
shading flat

But what if you wanted to show the two graphics in one figure window? Colormaps are properties of figures, so it's a bit more complicated to do that than you might think:

figure('color','w')
subplot(1,2,1)
image(X);
colormap(map);
subplot(1,2,2)
contour(P,15)
colormap(copper)
axis ij square
pcolor(P)
axis ij square
shading flat

Notice that the second call to colormap affects the image of the clown as well as that of the penny. There are ways to circumvent this behavior. Notably, you could combine the two colormaps into one, and then use different portions of the concatenated colormap for each graphic displayed. Or, if you have the Image Processing Toolbox and are dealing exclusively with images, you can use function subimage to visualize images with different colormaps.

But John's submission makes it considerably easier to combine graphics; just issue a freezeColors command after drawing the clown image, then generate the display of the penny:

figure('color','w')
subplot(1,2,1)
image(X);
colormap(map);
% Here's John's contribution:
freezeColors

subplot(1,2,2)
contour(P,15)
colormap(copper)
axis ij square
pcolor(P)
axis ij square
shading flat

I've been meaning to write a similar function for years. Now I don't have to. Thanks, John!

Comments? Leave them here.


Get the MATLAB code

Published with MATLAB® 7.8

12 CommentsOldest to Newest

I’ve used freezeColors for quite some time now. It proved realy useful for me, I am always working with medical images (grayscale) along with some ‘debug’ plots which simply show better using, say, ‘flag’ or ‘jet’. So I’ve created my specialised plot functions, making use of freezeColors.

Thanks for the comment, Luca. I don’t see your specialized plot functions on the File Exchange. Might others benefit from them? ;)
Brett

I have used and like freezeColors. It might be worth noting that the trick that freezeColors uses is to convert the color values to rgb…that way the colormap of the figure no longer applies. Once you know that, there are several tools to get multiple colormaps on the same figure or axes. I like to use Oliver Woodford’s FEX submission, SC (http://www.mathworks.com/matlabcentral/fileexchange/16233) to convert indexed images as well as cdata for surf plots into RGB. Here is a simple example:

[x,y,z]=peaks(50);

zpos=z;
zpos(z<=0)=NaN;
zrgb=sc(zpos,'hot');

figure
sh(1)=surf(x,y,z);
hold on
sh(2)=surf(x,y,zpos,zrgb);
set(sh,'edgecolor','none')

I love this entry too, and had my own kludges for years to make it happen, but Iverson’s solution is much more elegant.

It is unfortunate that this requires a workaround to include colorbars. I actually don’t understand the logic behind the new way the colorbar class is implemented. I love how you can place the colorbars, but having them redraw everytime a new graphic element is added seems unnecessary.

Of course all this fudging could be solved if colormaps were associated with axes rather than figures. It really seems like a holdover from the days when graphics cards couldn’t handle more than 256 colors. Seems like a simple change that would be easily backwards compatible.

Yes, this is a very useful contribution by John. By the way, to freeze the colorbar you can use my CBFREEZE:

% DATA
load clown;
load penny.mat

% FIGURE
figure('color','w')

subplot(1,2,1)
image(X);
colormap(map);
cbfreeze   % --------------> FREEZE COLORBAR
% Here's John's contribution:
freezeColors

subplot(1,2,2)
contour(P,15)
colormap(copper)
axis ij square
pcolor(P)
axis ij square
shading flat
colorbar

Download it here:
http://www.mathworks.com/matlabcentral/fileexchange/24371

Cheers, Carlos

Very nice function, indeed, thanks for that. But why does’t MATLAB freeze the colormaps for subplots automatically?

Cecilia,
If I recall correctly, John provided a way to do that:

h=colorbar; freezeColors(h)

…or simply: freezeColors(colorbar).

Or, take a look at Carlos’s very useful comment his CBFREEZE function, above.
Cheers,
Brett

Dear all,

I would like to paint N cylinders, each cylinder [in the same figure] with its own color map, according to an intensity function. Is it possible to do it with these cool freeze functions? [In my plot, the cylinders are always identical even if they have different intensities]

thanks,

Pedro

These postings are the author's and don't necessarily represent the opinions of MathWorks.