Matlab: How to assign different colormaps/colorbars to different surfaces in the same Figure

theenemy picture theenemy · Nov 10, 2011 · Viewed 36.8k times · Source

I am fairly new to Matlab and have a few questions. I got two surfaces and a plane in the same figure. I want to use different colormap and colorbar for b and another colormap and colorbar for c. s is fixed color so it's not a problem.

Let me try to explain what I am trying to achieve:

cmap1=colormap(topobathy) -->cmap1 is 64x3 double as expected

cmap2=colormap(redwhitegreen)

create cdata1 using cmap1 (this is the first part I cannot figure out, how to scale z data using cmap1, by default CData contains the z values)

b=surf(x,y,z,cdata1)

colorbar for b using z values

c=pcolor(x,y,(z-z0)) - I want to use cmap2 for this one.

colorbar for c using (z-z0) values

Here is what I have so far and the problems I encounter

b=surf(x,y,z);
colormap(topobathy);
cbar1=colorbar;
set(get(cbar1,'ylabel'),'String', 'Elevation (m)', 'Rotation', 90)
hold on;
s=surf(x,y,z1,'FaceColor',[0.278 0.788 0.788])
hold on;
change=z-z0;     
c=pcolor(x,y,change)
set(c,'ZData',100 + 0*change); %plotting it above the other surfaces
colormap(redwhitegreen)`

at this point colormap is set to redwhitegreen for b, colorbar of b I can't get the second colorbar with its own clim etc.

I used the freezeColors and cbfreeze explained in this link: http://blogs.mathworks.com/pick/2009/07/24/using-multiple-colormaps-in-a-single-figure/

but one thing works while messing another thing (probably all my fault). I want to learn how to have complete control over my objects without using external m files anyway.

Any help is appreciated.

Answer

John Colby picture John Colby · Nov 10, 2011

The basic idea is that you concatenate the colormaps, and then shift/scale the color data (CData) of the different plot handles to line up with the desired portions of the colormap. So, without knowing what your custom functions or specific data are, you could do something like colormap(topobathy(64); redwhitegreen(64)) and then scale the CData of b into the range [1,64] and the CData of c into the range [65,128].

There is an excellent guide on the MathWorks website that explains all this (even uses surf() and pcolor() like your example):

http://www.mathworks.com/support/tech-notes/1200/1215.html#Example_1

For the colorbar, you can just fake out the ticks and labels in a similar manner. Here is rough shot at making a color bar for the above example:

h = colorbar;
ticks = [1 16:16:64 64:16:128];
ticks(5:6) = [62 66];
set(h, 'YTick', ticks);

labels = num2str(repmat(linspace(min(Z(:)), max(Z(:)), 5), 1, 2)', 2);
set(h, 'YTickLabel', labels)

enter image description here