Colorbar in colormap with labels

2 ビュー (過去 30 日間)
erup eruo
erup eruo 2018 年 10 月 26 日
回答済み: erup eruo 2018 年 10 月 26 日
Hello,
I have this simple example where the colorbar is not working properly. For example, the color corresponding to 'j' is not shown wether the color coreesponding to 'h' (not present in the representing matrix) is.
labels = {'a','b','c','d','e','f','g','h','i','j','k','l'};
mtx = [2 5 4 5 6 3 2 5 2 2 2 4 6 7 9 10 9 5 9 7 10 2 3];
t = (0:1:length(mtx)-1)/5;
y = ones(1,length(t));
[C,ia,ic] = unique(mtx);
figure
colormap(jet(length(C)));
image(t,y,mtx); % or imagesc instead
yticklabels({});
colorbar('XTick',0:1:12,'TickLabels',labels,'Position',[0.95 0.1 0.01 0.5]);
Can anyone help me??
Tanks!!

採用された回答

jonas
jonas 2018 年 10 月 26 日
編集済み: jonas 2018 年 10 月 26 日
There range in your plotted values are 2-10, and the colorscale is per default scaled so that the maximum (10) and minimum (2) are the max and min values on your colormap. You need to force the colorbar to extend beyond these values.
labels = {'a','b','c','d','e','f','g','h','i','j','k','l'};
mtx = [2 5 4 5 6 3 2 5 2 2 2 4 6 7 9 10 9 5 9 7 10 2 3];
t = (0:1:length(mtx)-1)/5;
y = ones(1,length(t));
set(gca,'clim',[0 12])
figure
cmap=colormap(jet(numel(labels)));
image(t,y,mtx); % or imagesc instead
yticklabels({});
cb=colorbar('XTick',1.5:1:12.5,'TickLabels',labels,'Position',[0.95 0.1 0.01 0.5],'limits',[1 13]);

その他の回答 (3 件)

erup eruo
erup eruo 2018 年 10 月 26 日
That's right. But there is neither 12 nor 8 nor 11. With these limits, the labels don't correspond with the colors. Even though, 9 has the same colour than 10.
  2 件のコメント
jonas
jonas 2018 年 10 月 26 日
編集済み: jonas 2018 年 10 月 26 日
maybe if you change this line
colormap(jet(numel(labels)));
You need the same number of (1) colors in your colormap, (2) ticks on your colorbar and (3) number of labels
In your original script you had 12 ticklabels, 13 ticks and ~8-9 colors in your map. If you have more ticks than ticklabels, then the ticklabels start repeating. If you have less colors than ticklabels, then they will not match in the colorbar.
I guess this is what you might want as end-result
jonas
jonas 2018 年 10 月 26 日
Come on.. don't accept your own "answer" that isn't even an answer.

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


erup eruo
erup eruo 2018 年 10 月 26 日
Okey
thanks for your info
labels = {'a','b','c','d','e','f','g','h','i','j','k','l'};
mtx = [2 5 4 5 6 3 2 5 2 8 2 2 4 6 7 9 10 9 5 9 7 10 2 3 11 12];
t = (0:1:length(mtx)-1)/5;
y = ones(1,length(t));
figure
colormap(jet(numel(labels)));
image(t,y,mtx); yticklabels({});
colorbar('XTick',1.5:1:12.5,'TickLabels',labels,'Position',[0.95 0.1 0.01 0.5],'limits',[1 13]);
set(gca,'clim',[1 12]);
Fyi, this is how I wanted the final result.
THANK YOU VERY MUCH!!
  2 件のコメント
jonas
jonas 2018 年 10 月 26 日
Yep, you can thank me by accepting the answer I provided.
Image Analyst
Image Analyst 2018 年 10 月 26 日
Vote for it too, like I did.
People who help you can earn "reputation" points when people Accept their answers (4 points) or vote for them (2 points). You can do both if you were the original poster. See here: https://www.mathworks.com/matlabcentral/answers/help?s_tid=al_priv#reputation

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


erup eruo
erup eruo 2018 年 10 月 26 日
I'm sorry. I made a mistake. Obviously your answer is the good one

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by