필터 지우기
필터 지우기

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

조회 수: 20 (최근 30일)
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.

채택된 답변

Cris LaPierre
Cris LaPierre 2019년 3월 4일
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

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2019년 3월 2일
편집: Cris LaPierre 2019년 3월 2일
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
Cris LaPierre
Cris LaPierre 2019년 5월 6일
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 2019년 5월 6일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by