0

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
Brittney
  • 31
  • 3
  • I notice that you aren't applying your colors to the profile in the same order they're in the colormap. You set ColorData = 1 to Profile values below where ColorData = 0. So, the colors in the colorbar are going to be in the wrong order. – FragileX Jan 26 '23 at 16:37
  • I want to make sure I understand, so my ColorData(Profile<=breakpoint(1)) = 1 would change to ColorData(Profile<=breakpoint(1)) = 0 and the ColorData(Profile<=breakpoint(2)) = 2 would be correct, right? – Brittney Feb 05 '23 at 23:59
  • Well, your 2 assignment in your comment is different than in the question. Your ColorData is already all zeros. One way to accomplish what you want is first `ColorData(Profile>=breakpoint(1)) = 1` and second `ColorData(Profile>=breakpoint(2)) = 2`. With this way, you could even extend to many breakpoint and use a loop. – FragileX Feb 06 '23 at 02:22

1 Answers1

1

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 ;
FragileX
  • 626
  • 1
  • 8