MATLAB: How place the tick at the mid value of specific color range and uniform color bar across different ranges of data

colorcolorbarcolormapmapmidvalueplotrangetickuniform

Hi,
I want to place the tick at the mid value of the specific color range, for example, for red : 0-10, place the tick at 5, for blue color: 11-20 tick at 15 etc…
Also, I want to create uniform color bar across multiple datasets which have different ranges of values.
What I have so far does not work,
variable = Mis;
m_proj('Equidistant cylindrical','lat',[-85.044 85.044],'lon',[-180 180],'aspect',.2);
m_pcolor(Lon,Lat,variable);
h=colorbar('Ticks',[0,5,10,15, 20],...
'TickLabels',{'0','5','10','15', '20');
colormap(jet(5));
caxis([0 20])
Any help is appreciated.

Best Answer

I'll start a new anwer using a different approach. This time we'll manually define everything - no shortcuts. That should allow you more control over what is going on, making it easier for you to adapt.
Going back to your original question:
  • 0-10 = red, tick at 5
  • 11-20 = blue, tick at 15
  • etc.
First, create a custom colormap, manually specifying the colors and the range they apply to. Note that the ranges aren't equal (one has 5 in it). Not sure why 0-10 is 10 and not 11, but hey, it's working.
% Create custom colormap
cmap = [repmat([1 0 0],10,1) % >=0 & <=10 (red)
repmat([0 0 1],10,1) % >10 & <=20 (blue)
repmat([0 1 0],10,1) % >20 & <=30 (green)
repmat([1 1 0],5,1)]; % >30 & <=35 (yellow)
If you want more colors, add them to cmap. Just adjust the pattern to match the ranges you specify.
With that defined, plot your data. Then set the colormap, caxis limits, and ticks as follows:
colormap(cmap)
h=colorbar;
caxis([0 35])
set(h,'Ticks',[5 15 25 32.5])
Set the ticks to whatever you want to use. It will by default use its value as the label.
colorbarTicks_midRange_v3a.png
If you want the tick label to be different from the tick value, set the tick label property as well. For example, if I wanted the label to display in the middle of the range but show the max value of the range, I'd do this instead:
set(h,'Ticks',[5 15 25 32.5],'TickLabels',num2str([10; 20; 30; 35]))
colorbarTicks_midRange_v3.png