필터 지우기
필터 지우기

xticks and lables for figure with 2 y axes

조회 수: 1 (최근 30일)
Dam
Dam 2015년 1월 9일
편집: dpb 2015년 1월 12일
x, A,B and C are all vectors of 1050*1;
x=1:1050
(each 200 value of x represent one year so 1 have 5 years and 50 days) I use plot with 2 Y axis as follows
a=plot(x,A*100,'color','r')
b=plot(x,B*100,'color','black','linewidth',1.25)
ax2 = axes('Position',get(ax1,'Position'),...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
linkaxes([ax1 ax2],'x');
hold on
c=plot(x,C,'Parent',ax2,'color','b','linewidth',1.25);
What i need is to change default Xtiks to 5 and Xlables years 2009 to 204 written in the middle of the interval. methods for simple figure with 2 axis are not working! Thank you for help
  댓글 수: 1
dpb
dpb 2015년 1월 9일
What i need is to change default Xtiks to 5 and Xlables years 2009 to 204 written in the middle of the interval.
I can't decipher what you're asking for, sorry. Can you try to explain in more detail what you want the axes labels to be and where...oh, I think I got it--you've got a typo and the 204 above should have been 2014 I'll bet...
Use plotyy instead...see answer now that I think I figured out the question...

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

답변 (1개)

dpb
dpb 2015년 1월 9일
[hAx,hL]=plotyy(x,[A B]*100,x,C);
set(hAx(2),'xtick',[]); % don't show any ticks on 2nd x-axis
linkaxes(hAx,'x') % link everything to come
set(hAx(1),'xtick',[0:100:length(x)]) % need half 200 for the midpoint placement wanted
xtik=2009:2014; % the desired years
xtiklab=repmat(blanks(4),2*length(xtik),1); % a labels array space allocation
xtiklab(2:2:end)=num2str(xtk(:)); % every other is the year
set(hAx(1),'xticklabel',xtiklab) % and set 'em...
Salt to suit for the line colors, patterns, etc., using the appropriate handles in hL
  댓글 수: 2
Dam
Dam 2015년 1월 9일
편집: dpb 2015년 1월 10일
Thank you for your answer, could you please explain to me the numbers 4 and 2 in the line:
xtiklab=repmat(blanks(4),2*length(xtik),1); % a labels array space allocation
and then
xtiklab(2:2:end)=num2str(xtk(:)); % every other is the year
because i need to change the length of Vectors
Thank u in advance
dpb
dpb 2015년 1월 10일
편집: dpb 2015년 1월 12일
It's double the length of the number of years you're wanting on the axis in order to have twice the total number of tick marks. This is so that the even-numbered ones (2:2:end) can be labeled and be in the middle of the year as you requested. You see the blanks are in the odd locations beginning with the first; the numeric values in even.
ADDENDUM
The above uses the 'tick' and 'ticklabel' properties to label. Since you wanted the lablels to appear at the midpoint of the year instead at the beginning, two ticks are required/year; the midyear and beginning(/end of previous) so the mid-year ones can be used for labelling and the others left blank.
The alternative way to achieve the result is to have only the year start/end ticks but set the 'ticklabel' property to [] and write the labels via text at the midpoint x-axis positions. I just gave an example of writing y-ticks via text here--

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by