The following code produces a strip in the middle of a flat base. How can I adjust the color bar so that I can use only one color to represent the data range [0 2), one color for (2 4], one color for (4 6], and so on? So the color on the strip will be discrete on each level, rather than a continuous transition.
img = zeros(101,101);
img(51,51) = 10;
% let's try discrete color scale
surf(img)
shading interp
colorbar
axis tight

 採用された回答

Image Analyst
Image Analyst 2015 年 3 月 25 日

0 投票

Try this:
clc;
close all;
img = zeros(101,101);
img(51,51) = 10;
% let's try discrete color scale
surf(img)
shading interp
axis tight
myCustomColorMap = zeros(256, 3);
% Map 0-2, which is the first 20% or 51 elements:
row1 = 2;
row2 = row1 + 50;
myCustomColorMap(row1:row2,:) = repmat([1,0,0], [row2-row1+1, 1]); % Red
% Map 2-4, which is the next 20% or 51 elements:
row1 = row2+1;
row2 = row1 + 50;
myCustomColorMap(row1:row2,:) = repmat([0,1,0], [row2-row1+1, 1]); % Green
% Map 4-6, which is the first 20% or 51 elements:
row1 = row2+1;
row2 = row1 + 50;
myCustomColorMap(row1:row2,:) = repmat([0,0, 1], [row2-row1+1, 1]); % Blue
% Map 6-8, which is the first 20% or 51 elements:
row1 = row2+1;
row2 = row1 + 50;
myCustomColorMap(row1:row2,:) = repmat([1,1,0], [row2-row1+1, 1]); % Yellow
% Map 8-10, which is the first 20% or 51 elements:
row1 = row2+1;
row2 = size(myCustomColorMap, 1);
myCustomColorMap(row1:row2,:) = repmat([1,0,1], [row2-row1+1, 1]); % Magenta
colormap(myCustomColorMap);
colorbar

5 件のコメント

Kyle Wang
Kyle Wang 2015 年 3 月 25 日
Actually, I have some extra concerns about the colorbar.
img = zeros(101,101);
img(51,51) = 10;
% let's try discrete color scale
surf(img)
shading interp
axis tight
zlim([0 20])
Now I'm trying to create a colorbar with 4 scales: 0-5, 5-10, 10-15, 15-20. So the mid strip should have only two colors. Is it possible to do this?
Image Analyst
Image Analyst 2015 年 3 月 25 日
Sure. Just understand the code. The color map has 256 rows and 3 columns. Column 1 is the red value and is in the range 0-1. Column2 is green, and column 3 is the blue value. So that 256 rows spans your range of your data - 0 to 10 in your case. So just figure out what row corresponds to what value and set the color for that row. Does that explain it better?
Kyle Wang
Kyle Wang 2015 年 3 月 25 日
umumum, I understand somehow the color rules in matlab, still something wrong in my code. The strip has three colors instead of two. and the color ticks do not align with the color level?
img = zeros(101,101);
img(51,51) = 10;
% let's try discrete color scale
surf(img)
shading interp
axis tight
zlim([0 20])
colorLevel = 5;
colorMapSize = colorLevel * 50 + (colorLevel + 1);
myCustomColorMap = zeros(colorMapSize, 3);
grayColor = jet(colorLevel+1); % 1st = [0 0 0]
row1 = 2;
row2 = row1 + 50;
myCustomColorMap(row1:row2,:) = repmat(grayColor(2,:), [row2-row1+1, 1]);
for n = 2: colorLevel
row1 = row2+1;
row2 = row1 + 50;
myCustomColorMap(row1:row2,:) = repmat(grayColor(n+1,:), [row2-row1+1, 1]);
end
colormap(myCustomColorMap);
caxis([0 20])
hcb = colorbar;
set(hcb,'YTick',[0 5 10 15 20 25]);
Kyle Wang
Kyle Wang 2015 年 3 月 25 日
The line
grayColor = jet(colorLevel+1); % 1st = [0 0 0]
can also be
grayColor = gray(colorLevel+1); % 1st = [0 0 0]
Kyle Wang
Kyle Wang 2015 年 3 月 25 日
編集済み: Kyle Wang 2015 年 3 月 25 日
oops, just made the following change then the colorbar works fine:
colorLevel = 4;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

質問済み:

2015 年 3 月 24 日

編集済み:

2015 年 3 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by