Matlab - How to make graphs with different limits match using plotyy

5 views (last 30 days)
Kjetil Ovesen
Kjetil Ovesen on 3 May 2016
Answered: Ilham Hardy on 4 May 2016
I have two arrays that I want to plot with two axes. One using a bar-plot, and the second using regular line.
The left hand yaxis is area, and the left hand side is change in area in percent. X-axis is the date of the measurement.
My problem is that the second axis don't match the points of the bar plot. I want the points to be in the same x,y location of the plot.
if true
% code
Code:
date= [714262 724124 731733 734732 736209]; %matlab dates
area = [ 60154 48444 38991 29487 24084]; % area
area_change = [0 0.1947 0.3518 0.5098 0.5996 ]; %change in percent
figure(7)
[AX, T1, T2] = plotyy(date, area ,date, area_change, 'bar', 'line')
set(TX(2), 'ycolor', 'r')
set(TX(2),'YDir','reverse') %%reversed to show area decrease.
end

Answers (1)

Ilham Hardy
Ilham Hardy on 4 May 2016
Something like this? (not completely sure about the reverse part, though)
date= [714262 724124 731733 734732 736209]; %matlab dates
area = [ 60154 48444 38991 29487 24084]; % area
area_change = [0 0.1947 0.3518 0.5098 0.5996 ]; %change in percent
figure(7)
[AX, T1, T2] = plotyy(date, area ,date, area_change, 'bar', 'line');
set(AX(1),'xtick',date,'xticklabel',mat2cell(date));
set(AX(2),'xtick',date,'xticklabel',mat2cell(date));
aax = get(AX(1),'Xlim');
aay = get(AX(1),'Ylim');
set(AX(2),'XLim',aax,'YLim',aay.*1e-5,'YTickMode','auto','YGrid','on','XGrid','on')
set(AX(2),'xtick',date);
set(T2, 'color', 'r')
set(AX(2), 'Ycolor', 'r')
set(AX(2),'YDir','reverse') %%reversed to show area decrease.
aay_lbl = get(AX(2),'YTickLabel');
set(AX(2),'YTickLabel',flipud(aay_lbl)) %%reversed to show area decrease.

Community Treasure Hunt

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

Start Hunting!

Translated by