Location of discrete colorbar ticklabels

15 views (last 30 days)
Hi all,
I plotted an imnage with a custom color map and a discrete color bar with the following code (see code and figure below). However, the tick labels at the discrete colorbar are placed in the middle of the discrete sections. I would like these shifted to the upper part of the discrete bin edges, e.g. 2 at the white/light blue border, 4 at the light blue/darker blue border, etc. How can I do this?
Thanks!DLS_ex.png
figure;
my_colormap = [255,255,255
153,255,255
51,153,255
0,0,255
0,102,0
0,204,0
0,255,0
255,255,0
204,204,0
255,128,0
255,51,51
204,0,0
204,0,204
178,102,255
204,153,255
229,204,255
]./255;
binEdges = [0 2 4 6 10 15 20 25 30 35 40 50 60 70 80 100] ;
labels = textscan(num2str(binEdges),'%s') ;
labels = labels{1} ;
labels{length(labels)} = [labels{length(labels)} '%'] ;
[~,indices] = histc(shear_DLS_fin,binEdges);
indices(isnan(shear_DLS_fin)) = NaN ;
indices(isinf(shear_DLS_fin)) = NaN ;
[x1,y1] = meshgrid(x_as,y_as);
x_asfin = min(x_as):0.01:max(x_as);
y_asfin = min(y_as):0.01:max(y_as);
[x2,y2] = meshgrid(x_asfin,y_asfin);
imagesc(x_asfin,y_asfin,indices-1)
shading flat
axis equal tight
colormap(gca,my_colormap); % gca as first argument prevents
hold on
borders('Countries','LineWidth',3,'Color','black')
xlim([xmin xmax]);
ylim([ymin ymax]);
set(gca,'YDir','normal')
h = colorbar;
caxis([0 length(binEdges)-1])
h.YTickLabel = labels ;
box on
title('Deep layer shear 0-6 km (kt)');
numcolors = 16;
caxis([1 numcolors]);
cbarHandle = colorbar('YTick',...
[1+0.5*(numcolors-1)/numcolors:(numcolors-1)/numcolors:numcolors],...
'YTickLabel',int2str([1:numcolors]'), 'YLim', [1 numcolors]);
cbarHandle =colorbar('YTick',...
[1+0.5*(numcolors-1)/numcolors:(numcolors-1)/numcolors:numcolors],...
'YTickLabel', ...
{'2','4','6','10','15','20','25','30','35','40','50','60','70','80','100','>100'});

Answers (1)

Pranjal Kaura
Pranjal Kaura on 1 Sep 2021
Hey,
It is my understanding that you want to change the location of the tick labels in your image.
You can do this by changing the 'Ticks' property of the colorbar object ('h' in your case). For your code, you can try adding a positive constant to the default 'Ticks' array.

Community Treasure Hunt

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

Start Hunting!