Jin Yan
Jin Yan

Reputation: 199

how to plot four curves with one x axis and two y axis in one figure with MATLAB

I have four set of data: (x1,y1), (x1,y2), (x2,y3), (x2,y4). y1 and y3 as well as y2 and y4 are two pairs of numerical results and analytical results. So I want to compare them in a same figure. However, the magnitude of y1 and y3 is much larger than y2 and y4, so I need two y axis to show the curves clearly. x1 and x2 are sampled time instances but have different length. For example, x1 = 0:dt1:T, x2 = 0:dt2:T, where dt1 and dt2 are two different time step and T is the total time. I tried plotyy but I can only plot two curves in one figure instead of four. Does anyone have an idea? Thanks a lot!

Upvotes: 0

Views: 326

Answers (1)

David
David

Reputation: 8459

This example, virtually the same as given in the documentation for plotyy should help. You will just need to concatenate your data appropriately depending on how it is stored.

x = linspace(0,10);
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
y3 = 150*exp(-0.05*x).*sin(x);
y4 = 0.2*exp(-0.5*x).*sin(10*x);

figure
[hAx,hLine1,hLine2] = plotyy([x',x'],[y1',y3'],[x',x'],[y2',y4']);

EDIT: Also this question may be helpful as well.

Upvotes: 1

Related Questions