Text labeling with contourfm colorbar

4 views (last 30 days)
Hello All,
I have been trying to label a colorbar with text with a figure resulted from the contourfm, while I do have some success I cannot get the first text value to appear.
with a simple contourf it does show up but when i plot with contourfm then it misses values.
What I am after is for the colorbar to include all legend_Names2, staring with the first one on the far left.
I attach the following working code snippet
Data= magic(2); % mock data
legend_Names2={'Opt1','Opt2','Opt3','Opt4'}; % mock legend strings
%
R = georasterref('RasterSize', size(Data),'Latlim',[30 31], 'Lonlim', [2 3]);
contourfm(double(Data),R,'edgecolor','none')
colormap(flipud(jet(max(max(Data)))));
cbh=colorbar('southoutside');
cbh.XTick = [floor(min(xlim(cbh))):1:ceil(max(xlim(cbh)))]; % create Colobar ticks incrementaly by 1
cbh.TickLabels=legend_Names2;
test_controufm.png
Thank you all in advance.

Accepted Answer

Githin John
Githin John on 22 Jan 2020
The value of floor(min(xlim(cbh))) evaluates to 1. The min value of the colorbar is 1.25. So the Tick for 1 is outside the colorbar and hence not visible.
Try
cbh.XTick = linspace(min(xlim(cbh)),ceil(max(xlim(cbh))),4);
or something similar.
  1 Comment
George Lavidas
George Lavidas on 22 Jan 2020
Hello John,
Thank you very much, it works.
But if i may i have only a small question, just to clear it in my mind.
The matrix i have starts from 1 and that is why i used the floor() function, however you are totally right that the xbh lim is 1.25.
But in any case thank you very much, for the swift response.
Kind regards,
George

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!