1
votes

I'm trying to plot a line graph and a bar graph on the same y-axis:

figure; 
plotyy(Pert, Rfootvel(:,i+1), Pert, 0,'bar','plot');
hold(ax(1), 'on');
legend('Pert 1-8', 'Base');
ylim(ax(2), [0 1]);
title(['The avg pert velocity of the first step vs the avg base velocity, PP' num2str(j)]);

Unfortunately, setting the second y-axis limit like this doesn't affect the second y-axis. Matlab simply does what it thinks is best. However, I need to directly compare the two, so the axes need to be the same. Can anyone help here?

1
Don't you miss an output argument to plotyy ? Maybe you forgot to show it in your snippet. eg [ax,BarPlot,RegPlot] = plotyy(...) - Benoit_11
Maybe you're looking for linkaxes? - Dev-iL
@Benoit_11, that turned out to be the problem. Thank you for your help! - Luc Evertzen
Great! Do you mind if I write that as an answer so that the thread is closed for future reference? - Benoit_11
@Benoit_11 no not at all - Luc Evertzen

1 Answers

1
votes

In order to use ax(n) you need to provide plotyy with the right output arguments. In your case, you could use the following:

figure; 

%// Here BarPlot and RegPlot are not really needed so you could replace them with ~.

[ax,BarPlot,RegPlot] = plotyy(Pert, Rfootvel(:,i+1), Pert, 0,'bar','plot');

hold(ax(1), 'on');

legend('Pert 1-8', 'Base');
ylim(ax(2), [0 1]);
title(['The avg pert velocity of the first step vs the avg base velocity, PP' num2str(j)]);