xticks and lables for figure with 2 y axes
2 views (last 30 days)
Show older comments
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 Comment
dpb
on 9 Jan 2015
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...
Answers (1)
dpb
on 9 Jan 2015
[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 Comments
dpb
on 10 Jan 2015
Edited: dpb
on 12 Jan 2015
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--
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!