Is it possible to create this colormap and corresponding colorbar in Matlab?

6 views (last 30 days)
Hello,
I want to use something like this colorbar on my figure:
Which you can see every 25 by 25 the color has changed. Now I want:
-------------------------------------------------
0-25 = #fff496
25-50 = #fcd70d
50-75= #adff2f
75-100 = #45ff2f
100-125 = #9cb63b
125-150 = #339445
150-175 = #1aa05b
175-200 = #17baa7
200-225 =#159fd0
225-250 = #2f52a4
250 - 275 = #2f449d
275-300 = #4f499f
300-325 = #7550a0
325-350 = #50376d
more than 350 = #2b1d3a
---------------------------------------------
I don't know how to set something like this. Here is my code:
S = shaperead ('country_Boundary.shp');
lon = S.X;
lat = S.Y;
plot(lon, lat, '-k')
hold on
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};
colorbar
% define start and end colors Using the hex -> rgb converter from this answer: https://www.mathworks.com/matlabcentral/answers/458086-how-to-specify-line-color-using-a-hexadecimal-color-code
start_color = sscanf('00ffff','%2x%2x%2x',[1 3])/255;
end_color = sscanf('0000ff','%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)
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;
caxis([min(z) max(z)])
I attached my data too. Thank you all.
  2 Comments
BN
BN on 6 May 2020
Hi, Thank you so much for your suggestion. I download and used it. But I saw it resulted in the wrong. I replaced this part:
start_color = sscanf('00ffff','%2x%2x%2x',[1 3])/255;
end_color = sscanf('0000ff','%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)
By:
num_colors = 100;
% create a map linearly interpolating between them
% i.e. three columns interpolating from Red1 to Red2, etc.
mycolormap = customcolormap(linspace(0,1,15), {'#fff496','#fcd70d','#adff2f',...
'#45ff2f','#9cb63b','#339445','#1aa05b','#17baa7','#159fd0','#2f52a4',...
'#2f449d','#4f499f','#7550a0','#50376d','#2b1d3a'});
colorbar('southoutside');
colormap(mycolormap);
But the resulted figure's colormap is wrong when I check it with my table data.
Here is the complete code that I used:
S = shaperead ('country_Boundary.shp');
lon = S.X;
lat = S.Y;
plot(lon, lat, '-k')
hold on
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};
% define start and end colors
start_color = sscanf('00ffff','%2x%2x%2x',[1 3])/255;
end_color = sscanf('0000ff','%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.
mycolormap = customcolormap(linspace(0,1,15), {'#fff496','#fcd70d','#adff2f','#45ff2f',...
'#9cb63b','#339445','#1aa05b','#17baa7','#159fd0','#2f52a4','#2f449d','#4f499f',...
'#7550a0','#50376d','#2b1d3a'});
colorbar('southoutside');
colormap(mycolormap);
% 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)
zlim = [min(z) max(z)]+[-0.1 +0.1];
clr_val = @(z) mycolormap(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;
caxis([min(z) max(z)])
axis off;
Do you know what is the problem?
Thank you

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 6 May 2020
Note that since R2019b, you can write literal hexadecimal (and binary) numbers directly into matlab.
I don't have the mapping toolbox to test with your data, but the following seems to be what you're after:
hexmap = [
0xfff496
0xfcd70d
0xadff2f
0x45ff2f
0x9cb63b
0x339445
0x1aa05b
0x17baa7
0x159fd0
0x2f52a4
0x2f449d
0x4f499f
0x7550a0
0x50376d
0x2b1d3a]; % I used notepad++ in column mode to extract your hex values and append the 0x
cmap = im2double(reshape(typecast(hexmap, 'uint8'), 4, [])'); %convert 32-bit 00RRBBGG hexadecimal into 8-bit quadruplets (GG, RR, BB, 00) then rescale to 0:1
cmap = fliplr(cmap(:, 1:3)); %reorder into R G B triplets.
%demo image just for testing
figure; imagesc(toeplitz(0:400));
%apply colormap with correct scale and display with correct tick marks.
colormap(cmap);
caxis([0 375]); %has to be that to match your spacing
colorbar('Ticks', 0:50:350);

More Answers (0)

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!