Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple colormaps

Status
Not open for further replies.

pcpuck

Electrical
Joined
May 16, 2003
Messages
1
Location
US
I would like to use different colormaps in different subplots. This simple example doesn't seem to work:
%%%%%%%%%%%%%%%%%%%%%%%
P=peaks(40);
C=del2(P);

subplot(1,2,1)
surf(P,C)
colormap white
colorbar

subplot(1,2,2)
surf(P,C)
colormap hot
colorbar
%%%%%%%%%%%%%%%%%%%%%%%%
Matlab always uses the last defined colormap, but I would like to use a different one for each subplot.

Can anyone help, please.

/Peter
 
Here's your code rewritten

Cheers

%%%%%%%%%%%%%%%%%%%%%%%
colormap('default');

P=peaks(40);
C=del2(P);

ax1=subplot(1,2,1);
surf(P,C)
% colormap white % Sets the colors to black and white on the figure
cbar1=colorbar;

ax2=subplot(1,2,2);
surf(P,C)
colormap hot % Changes all the colors on the figures
cbar2=colorbar;

% A colormap is an m-by-3 matrix of real numbers between 0.0 and 1.0.
% Each row is an RGB vector that defines one color. The kth row of
% the colormap defines the k-th color, where
% map(k,:) = [r(k) g(k) b(k)]) specifies the intensity of
% red, green, and blue. Color is obtained by mapping the color data (C)
% into the color map indices using

% colormap_index = fix((color_data-cmin)/(cmax-cmin)*cm_length)+1

% In the normal case, min(cdata) maps into 1, and max(cdata) maps into
% size(colormap,1). To have different color maps in different plots on a
% figure, Cmin and Cmax of each plot modified so that min(cdata) and max(cdata)
% maps into the start and end of the appropriate subsection of the colormap.
% We can do this by solving the above equation for cmin and cmax for two pairs
% of (color_data, colormap_index). For a composite color map, we know the
% range of the color map (colormap_index_min and colormap_index_max) and the
% range of the color data (color_data_min and color_data_max), the above
% equation can be solved for cmin and cmax.

% cmin = ( (colormap_index_max-1) color_data_min
% - (colormap_index_min-1) color_data_max )
% / (colormap_index_max - colormap_index_min)
% cmax = ( (colormap_index_max-1) color_data_min
% - (colormap_index_min-1) color_data_max
% + cm_length (color_data_max - color_data_min) )
% / (colormap_index_max - colormap_index_min)

% To use different color maps for different plots in a figure, you need to
% modify the color map so that it contains all the color maps for the
% plots on the figure. Also, you need to modify cmin and cmax (which come
% from CLim) of each plot so the plot maps cdata the proper index range of the color
% map.

% The composite color map can be created by stacking the desired color maps
% on top of each other. Keep track of the range of the color maps

cmap1=bone;
cmap2=jet;
colormap([cmap1;cmap2]);
cm_length = size(get(gcf,'colormap'),1);
range(1,1:2) = [1 size(cmap1,1)];
range(2,1:2) = [1 size(cmap2,1)]+range(1,2);
colormap_index_min = range(:,1);
colormap_index_max = range(:,2);
color_data_range(1,:) = get(ax1,'CLim');
color_data_range(2,:) = get(ax2,'CLim');
color_data_min = color_data_range(:,1);
color_data_max = color_data_range(:,2);


cmin = ( (colormap_index_max-1) .* color_data_min ...
- (colormap_index_min-1) .* color_data_max ) ...
./ (colormap_index_max - colormap_index_min);
cmax = ( (colormap_index_max-1) .* color_data_min ...
- (colormap_index_min-1) .* color_data_max ...
+ cm_length * (color_data_max - color_data_min) ) ...
./ (colormap_index_max - colormap_index_min);

% Set the cmin and cmax of the plots on this figure

set(ax1,'CLim',[cmin(1),cmax(1)]);
set(ax2,'CLim',[cmin(2),cmax(2)]);

% The colorbars are managed differently since the
% color data mapping is set to direct. This means that the values
% of the color data are indices in the color map.

set(get(cbar1,'children'),'CData',[colormap_index_min(1):colormap_index_max(1)]');
set(get(cbar2,'children'),'CData',[colormap_index_min(2):colormap_index_max(2)]');
%%%%%%%%%%%%%%%%%%%%%%%%
 
I am ploting multiple 3D surfaces in the same figure. Each surface has a different data range. I want each to have its own colormap. The examples in MatLab help deal with having multiple axes, and setting CLim differently for each axes after concatonating colormaps.

With only 1 axes and multiple surfaces how can I have each surface with its own colormap. Or at least each surface with its data range mapped into a color range in the one colormap so that each surface shows the variaiton in its data?

thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top