user4962385
user4962385

Reputation:

Matlab - plot bar and line graph on the same y axis

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?

Upvotes: 1

Views: 2190

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

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)]);

Upvotes: 0

Related Questions