필터 지우기
필터 지우기

Discrete color scale in 3D plot

조회 수: 17 (최근 30일)
Kyle Wang
Kyle Wang 2015년 3월 24일
편집: Kyle Wang 2015년 3월 25일
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일
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일
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개)

카테고리

Help CenterFile Exchange에서 Blue에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by