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

21 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 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:
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

Plus de réponses (1)

Cris LaPierre
Cris LaPierre le 2 Mar 2019
Modifié(e) : Cris LaPierre le 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
surf(peaks)
% Create a discrete colormap
cmap=jet(4);
colormap(cmap)
h=colorbar;
% Set tick marks to be middle of each range
dTk = diff(h.Limits)/(2*length(cmap));
set(h,'Ticks',[h.Limits(1)+dTk:2*dTk:h.Limits(2)-dTk])
colorbarTicks_midRange.png
  17 commentaires
Cris LaPierre
Cris LaPierre le 6 Mai 2019
I understand your question. I am not able to duplicate the behavior using data I create. Since I can't run your code, I can't tell you why you aren't seeing the behavior you expect.
Inspect this code. You'll see the transition from olive to dark green happens at 720.
x=715:.25:725;
X = meshgrid(x);
surf(X)
cmap = [
repmat([0.5 0.5 0.5],380,1) % gray
repmat([0.3010, 0.7450, 0.9330],100,1) % light blue
repmat([1 0 1],100,1) % magenta
repmat([0.3940, 0.1840, 0.5560],100,1) % purple
repmat([1 1 0],10,1) % yellow
repmat([0.8500, 0.5250, 0.0980],10,1) % orange
repmat([0.8 0 0],10,1) % red
repmat([0.4660 0.6740 0.1880],10,1) % olive green
repmat([0 0.45 0],10,1)% dark green
repmat([0 0.2470 0.6410],10,1)]; %(dark blue)
colormap(cmap)
h=colorbar;
caxis([0 740])
nlm
nlm le 6 Mai 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Orange dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by