Assign custom interval to the color map z axis of a surf plot

My surf (z) plot ranges from 0 to 255. i used turbo color map but the ranges per color were quite even across the value. i would like to able to set uneven custom interval for the colormap of my surf plot. I have checked through resources online, but could not figure it out for my case. Thanks in advance.

5 Comments

Shjow us your expectatons with a pictorial example.
I don't have a pictorial sample of what I expect.. but the present colour bar i have is attached. the intervals were divided evenly to 255/7 (because there were 7 colours).
However, I want to be able to set a customed division of intervals for the colour map. For example, 0-20, 20-100, 100-155, 155-231, 231- 240, 240-250, 250-255.
For what daya you want to use the colormap?
The data is quite larger than 5mb. i could not upload it. I just need an example. I can adapt it to my case
surf(peaks)
colormap turbo(7)
colorbar
The above is an example. However, I want to be able to use a custom interval. Instead of default range of even division of interval for the colors, I want a custom one.

Sign in to comment.

 Accepted Answer

hello
try my little demo
the colorbar is "frozen" so that ticks / tickslabels do not vary acccording to data range. In other words , you can display data which max value can be either below or above the max value of the thresholds without graphical impact.
see the 2 examples shown below
hope it helps
Z = 5*abs(Z); % modify here the magnification factor to see effects on plot
Z = 50*abs(Z); % modify here the magnification factor to see effects on plot
[X,Y,Z] = peaks(50);
Z = 50*abs(Z); % modify here the magnification factor to see effects on plot
[maxval, ~] = max(Z(:));
[minval, ~] = min(Z(:));
maxval = ceil(maxval);
minval = floor(minval);
thresholds = [0 20 100 155 231 240 250 255]; % absolute thresholds for data ( 0-20, 20-100, 100-155, 155-231, 231- 240, 240-250, 250-255)
n_thr = numel(thresholds);
mycolormap = jet(n_thr); % NB : Z data can be higher than max value of thresholds
colormap(mycolormap);
% create scaled color values C
C = zeros(size(Z));
for ck=1:n_thr-1
C(Z >= thresholds(ck) & Z < thresholds(ck+1)) = ck; %
end
C(Z >thresholds(end)) = ck+1; % do not forget data higher than max (last) value of thresholds
% plot
figure(1)
surf(X,Y,Z, C);
caxis([1 n_thr]); % freeze C axis (color) range
axis([-3 3 -3 3 0 maxval]);
cb_yticklabels = arrayfun(@num2str,thresholds,'uni',false);
cbh = colorbar;
set(cbh,'ytick',1+(n_thr-1)*(0:n_thr-1)/n_thr);
set(cbh,'yticklabel',cb_yticklabels);

4 Comments

Thanks @Mathieu NOE. However, I could not understand this. I am not looking to transform the "peaks" data to values ranging from 0 - 255. NO.
I only gave a sample plot. the values are from -6 to +8 wilh evenly divided interval. what if i interval i want is -6 to -5, -5 to -3.5, -3.5 to +0.5, +0.5 to +2.3, +2.3 to +4, +4 to +7.8, and +7.8 to 8. How do i go about it
hello again
for my demo I simply followed your inputs :
My surf (z) plot ranges from 0 to 255.
and
I want to be able to set a customed division of intervals for the colour map. For example, 0-20, 20-100, 100-155, 155-231, 231- 240, 240-250, 250-255.
I f you want now to change the data range and / or the thresholds values in the colorbar that is something you can do in my code . The code by itself does not need any change
all the best

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!