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

44 views (last 30 days)
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);
h=colorbar('Ticks',[0,5,10,15, 20],...
'TickLabels',{'0','5','10','15', '20');
caxis([0 20])
Any help is appreciated.

Accepted Answer

Cris LaPierre
Cris LaPierre on 4 Mar 2019
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:
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.
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]))

More Answers (1)

Cris LaPierre
Cris LaPierre on 2 Mar 2019
Edited: Cris LaPierre on 2 Mar 2019
Don't have your functions, so here's a quick mockup of how I might do it.
% Use data set included in MATLAB
% Create a discrete colormap
% Set tick marks to be middle of each range
dTk = diff(h.Limits)/(2*length(cmap));
nlm on 6 May 2019
Yes, I see it. I'm not sure why it is happening in my code then. The cmap created is the same in my code and yours.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by