help with using plotyy

I want to plot four sets of data using plotyy. The four sets are two sets of experimental data and the other two are predicted (calculated) values. Therefore, they would have different sizes. Ex, the experimental data for x and y are 13 points, while for predicted value, I have x=0:100:1. Therefore I can not use the plotyy with matrix method. Could anyone help with this? Thanks in advance.

 Accepted Answer

So you want one predicted/experimental pair on one axis, and another predicted/experimental pair on the other axis? If so, plot both predicted sets with plotyy, save the handles to the resulting axes, and then plot the experimental datasets:
x{1} = linspace(0,10,13);
y{1} = rand(1,13);
x{2} = linspace(0,10,100);
y{2} = rand(1,100);
x{3} = linspace(0,10,13);
y{3} = rand(1,13) * 100;
x{4} = linspace(0,10,100);
y{4} = rand(1,100) * 100;
[hax, hln(1,1), hln(2,1)] = plotyy(x{1}, y{1}, x{3},y{3});
hold(hax(1), 'on');
hold(hax(2), 'on');
hln(1,2) = plot(hax(1), x{2}, y{2}, 'o');
hln(2,2) = plot(hax(2), x{4}, y{4}, 'x');

1 Comment

Thanks! This works perfectly! Cheers!

Sign in to comment.

More Answers (1)

plotyy is really only needed if your different y values have very different scales (e.g. number of employees and total revenue over time). Because you are doing a fit, I am guessing that your y values are about the same scale.
Therefore, I think you really only need the ability to plot two lines on the same plot (even if they different lengths.) There are several ways to accomplish that. Here is one simple example.
% Pretend data with 13 points
data_x = rand(1,13);
data_y = rand(1,13);
% Pretend fit with 100 points [NOT AN ACTUAL FIT. JUST MADE UP!]
fit_x = linspace(0,1);
fit_y = 0.5*ones(size(fit_x)) + 0.1*fit_x.^2;
% Figure with both data and "fit"
figure
hold on
plot(data_x,data_y,'.')
plot(fit_x,fit_y)

3 Comments

Thank you for the reply, but actually I have two y axis. The predicted and experimental values are at the same scale, but there are two sets which have very different scale. Also, I am hoping that to be able to color the y axis in different colors to identify them easily. Any idea? I have been tried to use the line function with four lines, but would not be able to put the legend for all four lines--only the last one would show.
I agree with the cyclist. If you're comparing predicted values to measured values, you should not use plotyy. If the y scales are so vastly different between predicted and measured values, that's probably an issue worth depicting clearly, letting the viewer directly compare the different values. Use
plot(x,predicted_y,'k-')
hold on
plot(x,measured_y,'ro')
yeah...I kinda have to do that way.

Sign in to comment.

Categories

Asked:

on 31 Mar 2015

Commented:

on 2 Apr 2015

Community Treasure Hunt

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

Start Hunting!