How can I set correctly the colorbar?

조회 수: 2 (최근 30일)
Szabó-Takács Beáta
Szabó-Takács Beáta 2015년 9월 7일
댓글: Image Analyst 2015년 9월 8일
I tried to set a colorbar according to the following way:
m1 = [101 255 255]./255;
m2 = [207 170 85]./255;
m3 = [255 207 0]./255;
m4= [255 255 101]./255;
m5 = [203 0 203]./255;
map=[m1;m2;m3;m4;m5];
labels={'ET'; 'BSk'; 'BWh'; 'BWk'; 'Dfc'};
h=colorbar;
set(h,'YTickMode','manual','YTick',[1:length(map)],'YTickLabelMode','manual','YTickLabel',labels);
But in the figure only 'BWk' is marked in the midle of colorbar. How can I set correctly the colorbar?

채택된 답변

Image Analyst
Image Analyst 2015년 9월 7일
A few problems. You don't want your y tick numbers to go from 1 to 5. You want them to go from 0 - 255 or between the min and max of your image. As it is, with no data displayed, your caxis (the range) is 0 to 1, yet your tick marks go from 1 to 5. So only the one at 1 will appear - the rest are off the top of the scale and not displayed or visible. See corrected code:
imshow('cameraman.tif');
m1 = [101 255 255]./255;
m2 = [207 170 85]./255;
m3 = [255 207 0]./255;
m4= [255 255 101]./255;
m5 = [203 0 203]./255;
map=[m1;m2;m3;m4;m5];
labels={'ET'; 'BSk'; 'BWh'; 'BWk'; 'Dfc'};
colormap(map); % Apply the colormap
h=colorbar;
set(h,'YTickMode','manual','YTickLabelMode','manual');
length(map)
cMapRange = caxis();
xTickNumbers = linspace(cMapRange(1), cMapRange(2), size(map, 1));
set(h,'YTick',xTickNumbers,'YTickLabel',labels);
  댓글 수: 2
Szabó-Takács Beáta
Szabó-Takács Beáta 2015년 9월 8일
Thank you for your help! I tried it and it works hovewer only 'Dfc' is missing in colorbar. How can I insert it too?
Image Analyst
Image Analyst 2015년 9월 8일
Dfc is not missing from mine:
I don't know why it's missing from yours. Try my code or else check your code more carefully. Or maybe try resizing your figure.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 9월 7일
colorbar() uses y values that are within the caxis limits. If your caxis limits (which are usually determined automatically from your data) do not happen to include values in your tick range 1 to 5, then the corresponding label will not show up in the graph.
For example use
surf(2.1 + 2*rand(50,50));
and then your code. The data values do not include 1 or 2 or 5, so the automatic caxis limits will run from just over 2.1 to just under (2.1+2 = 4.1), leaving only the ticks 3 and 4 within the range to be drawn by colorbar, so only those two labels show up.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by