How can I set correctly the colorbar?
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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 个评论
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!