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

조회 수: 44(최근 30일)
BN 2020년 5월 6일
댓글: BN 2020년 5월 6일
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
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:
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
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표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
BN 2020년 5월 6일
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.
'#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:
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.
'#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

댓글을 달려면 로그인하십시오.

### 채택된 답변

Guillaume 2020년 5월 6일
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
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);
##### 댓글 수: 1표시 없음숨기기 없음
BN 2020년 5월 6일
Thank you so much it worked!

댓글을 달려면 로그인하십시오.

### 범주

Find more on Colormaps in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by