How can I set correctly the colorbar?

1 vue (au cours des 30 derniers jours)
Szabó-Takács Beáta
Szabó-Takács Beáta le 7 Sep 2015
Commenté : Image Analyst le 8 Sep 2015
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?

Réponse acceptée

Image Analyst
Image Analyst le 7 Sep 2015
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 commentaires
Szabó-Takács Beáta
Szabó-Takács Beáta le 8 Sep 2015
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 le 8 Sep 2015
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 7 Sep 2015
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.

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by