Set Colorbar TickLabels and TickMarks

556 ビュー (過去 30 日間)
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017 年 8 月 10 日
コメント済み: Sebastian Lopez 2020 年 11 月 5 日
Hi all,
I am having trouble setting my colorbar ticklabels as I wish.
Specifically I have 8 points, and have created a colormap such that these 8 points get values from the darkest to the lightest color in the colormap. Similarly, I would like my colorbar's labels to follow the same pattern. Below is the code that I have written:
cmapdef = colormap(winter) ; %Define Colormap
cmap = cmapdef(1:8:end, :) ; %Find Values of colors corresponding to each point plotted
cbh = colorbar('YTickLabel', num2cell(1:8)) ;
However, the resulting colorbar has labels going as follows: 1 2 3 4 5 6 7 8 1 2 3. Instead I would expect the whole span of the color bar to be covered by 1 2 3 4 5 6 7 8.
What am I doing wrong?
Thanks in advance for your responses,
KMT.
  2 件のコメント
José-Luis
José-Luis 2017 年 8 月 10 日
Do you mean you want colored text in your colorbar?
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017 年 8 月 10 日
編集済み: Konstantinos Tsitsilonis 2017 年 8 月 10 日
Hi Jose,
I would not like to have colored text. It is just that tick labels appear on the color bar on the following order: 1 2 3 4 5 6 7 8 1 2 3, instead of the normal 1 2 3 4 5 6 7 8.
Thanks anyway!

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

採用された回答

Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017 年 8 月 10 日
編集済み: Konstantinos Tsitsilonis 2017 年 8 月 10 日
Actually I managed to find a fix.
All the ticks range from 0 to 1, and they should have a tick label associated to them.
Therefore the following code solves the problem:
cmap = colormap(winter(8)) ; %Create Colormap
cbh = colorbar ; %Create Colorbar
cbh.Ticks = linspace(0, 1, 8) ; %Create 8 ticks from zero to 1
cbh.TickLabels = num2cell(1:8) ; %Replace the labels of these 8 ticks with the numbers 1 to 8
  2 件のコメント
Peng He
Peng He 2019 年 6 月 26 日
That code only put one tick for me : (
Sebastian Lopez
Sebastian Lopez 2020 年 11 月 5 日
Works perfect! Thank you

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

その他の回答 (1 件)

José-Luis
José-Luis 2017 年 8 月 10 日
plot(rand(10,1))
cH = colorbar;
cH.TickLabelInterpreter = 'tex';
idx = 955;
for ii = 1:numel(cH.TickLabels)
cH.TickLabels{ii} = [sprintf('\\color[rgb]{%f,%f,%f} ', rand(1,3)), char(idx)];
idx = idx + 1;
end
You'd need to replace the random color with the actual color you want.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by