How to create a custom colormap in this case?

I want to know how I can change the color map and corresponding color bar to showing from light blue(#add8e6) to dark blue (#00008b).
S = shaperead ('country_Boundary.shp');
lon = S.X; % for example, I want to find all points in polygon number one
lat = S.Y;
plot(lon, lat, '-k')
hold on
axis equal
ax = gca;
xL = ax.XLim;
yL = ax.YLim;
rect_x = [-0.25 -0.25 0.25 0.25];
rect_y = [0.25 -0.25 -0.25 0.25];
x = points{:,1};
y = points{:,2};
z = points{:,3};
num_colors = 100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clrs = summer(num_colors); %<<<< here is the problem
zlim = [min(z) max(z)]+[-0.1 +0.1];
clr_val = @(z) clrs(ceil(interp1(zlim, [0 1], z)*num_colors), :);
for i=1:numel(x)
p(i) = patch(rect_x + x(i), rect_y + y(i), ...
clr_val(z(i)), ...
'EdgeColor', 'none');
end
ax.XLim = xL;
ax.YLim = yL;
colorbar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
colormap(summer)
caxis([min(z) max(z)])
Thanks

 Accepted Answer

Sindar
Sindar on 6 May 2020
Edited: Sindar on 6 May 2020
That is essentially the example here.
Using the hex -> rgb converter from this answer:
% test image
imagesc(rand(10,10))
colorbar
% define start and end colors
start_color = sscanf('add8e6','%2x%2x%2x',[1 3])/255;
end_color = sscanf('00008b','%2x%2x%2x',[1 3])/255;
N = 10;
% create a map linearly interpolating between them
% i.e. three columns interpolating from Red1 to Red2, etc.
map=[linspace(start_color(1),end_color(1),N)' linspace(start_color(2),end_color(2),N)' linspace(start_color(3),end_color(3),N)'];
colormap(map)

6 Comments

If you have start and end colors that are more different (e.g., Yellow-Red), you'll want to think about color theory a bit more.
Additionally, this can be done in a GUI, see here
Dear Sindar,
I tried to use it but this error appears:
Index exceeds the number of array elements (18).
Also, I would like to have the border range of blue color from light blue to dark blue.
Thank you
Sindar
Sindar on 6 May 2020
Edited: Sindar on 6 May 2020
Try N=100. Also, this code already uses the colors you provided.
(or, better, replace N with num_colors in the code)
% test image
imagesc(rand(10,10))
colorbar
% define start and end colors
start_color = sscanf('add8e6','%2x%2x%2x',[1 3])/255;
end_color = sscanf('00008b','%2x%2x%2x',[1 3])/255;
num_colors = 100;
% create a map linearly interpolating between them
% i.e. three columns interpolating from Red1 to Red2, etc.
clrs =[linspace(start_color(1),end_color(1),num_colors)' linspace(start_color(2),end_color(2),num_colors)' linspace(start_color(3),end_color(3),num_colors)'];
colormap(clrs)
Note: from the error, it seems like you were trying to index the custom map directly:
clrs = map(num_colors);
The built-in colormaps (like summer) have a function that returns a colormap based on the number of colors you want. It is possible to replicate this behavior (by taking my code and putting it in a function of num_colors), but if you are directly building a map, it is an Nx3 matrix so you can't (and don't need to) tell it how many elements you want.
Thank you so much. I'll be grateful if you can look at another question of mine which is really similar. here is the link: https://www.mathworks.com/matlabcentral/answers/523398-is-it-possible-to-create-this-colormap-and-corresponding-colorbar-in-matlab
Thank you again

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Tags

Asked:

BN
on 6 May 2020

Commented:

BN
on 6 May 2020

Community Treasure Hunt

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

Start Hunting!