Creating surface map in Matlab with 2-3 user defined break point colorbar

76 Views Asked by At

I'm trying to create a code in Matlab that will produce a surface map that has a colorbar defined at 2 or 3 different break points, for example below 0.8 it will be white, from 0.8-1.2 it will be green, and greater than 1.2 will be blue (or 0.6, 0.8, 1 for 3 breakpoint). I have a code that will run with one defined breakpoint but am having troubles figuring out how to run it for multiple breakpoints. They need to be a defined singular color without a gradient transition in the colorbar. Any tips on what I can do to define the 2-3 breakpoint colorbar at user defined breaks?

%% Two Toned Map
% define colormap and breakpoint
cmap = [0 0 1 ; 1 1 1; 1 0 1];    
breakpoint = [0.7 ; 1.2]; %CHANGE VALUE

%create a color matrix with only 2 values:
%   where Profile < breakpoint => ColorData=0
%   where Profile > breakpoint => ColorData=1
ColorData= zeros(size(Profile)) ;
ColorData(Profile>=breakpoint(2)) = 2 ;
ColorData(Profile<=breakpoint(1)) = 1 ;

%Plot the surface, specifying the color matrix we want to have applied
hs = surf( xa, ya, Profile, ColorData, 'EdgeColor','none' ) ;
colormap(cmap) ;
hb = colorbar ;
set (gca, 'xdir', 'reverse')
set (gca, 'ydir', 'reverse')
set (gca, 'DataAspectRatio',[1 1 1])
xlim([0 80+deltax])
ylim([0 100+deltay])

%Now adjust colorbar ticks and labels
cticks = [0.25 0.5 0.75] ; % positions of the ticks we keep

% build labels
bpstr = num2str(breakpoint) ;
cticklabels = {['<' bpstr] ; bpstr ; ['>' bpstr]}

% apply
hb.Ticks = cticks ;
hb.TickLabels = cticklabels ;
title('Sheared 4mm') %CHANGE VALUE
1

There are 1 best solutions below

0
On

You need to modify the label-constructing code to handle more than one breakpoint.

%Now adjust colorbar ticks and labels
cticks = (1:numel(breakpoint))*(numel(breakpoint)/(numel(breakpoint)+1));
cticklabels = breakpoint;

% apply
hb.Ticks = cticks ;
hb.TickLabels = cticklabels ;