Customize Colormap with unequal range of colors.
25 views (last 30 days)
Show older comments
I want to have a colormap for values ranging from 0 to 5. I want values from 0 to 1.95 to be of 'white' color, 1.95 to 2.05 to be of 'blue' and 2.05 to 5 to be of 'yellow'. Its urgent. Thanks in advance!
0 Comments
Answers (1)
Karim
on 24 Dec 2022
Edited: Karim
on 24 Dec 2022
% first create some plot data, we use the matlab peaks as example
[X,Y,Z] = peaks(100);
% rescale the Z values so that they are between 0 and 5
Z = Z + abs(min(Z(:)));
Z = (Z./max(Z(:)))*5;
% create a 'color range', we need to sample between the min and max value of
% the plot and divide the range using the greatest common divisor of the
% desired ticks
col_range = 0:0.05:5;
% in our range look for the indexes for the colors we want
white_idx = col_range < 1.95;
blue_idx = col_range >= 1.95 & col_range < 2.05;
yellow_idx = col_range >= 2.05;
% initialize the color map i.e. as many rows as ticks and 3 columns (for
% the rgb values)
col_map = zeros(numel(col_range),3);
% set the colors using our previous indexes
col_map(white_idx,:) = repmat( [1 1 1] ,sum(white_idx),1);
col_map(blue_idx,:) = repmat( [0 0 1] ,sum(blue_idx),1);
col_map(yellow_idx,:) = repmat( [1 1 0] ,sum(yellow_idx),1);
% create the figure
figure
surf(X,Y,Z,'EdgeAlpha',0.25)
% activate the colormap
colormap(col_map)
% get the handle for the colorbar
col_handle = colorbar;
% set the ticks to the desired values
col_handle.Ticks = [0 1.95 2.05 5];
0 Comments
See Also
Categories
Find more on Orange in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!