Four data sets on one plot

1 次查看(过去 30 天)
Derek
Derek 2011-6-23
Hey guys, I have four data sets that I am trying to put on on plot. My code is as follows,
L=[0 10 20 30 40 50 60 70 80 90 100]; Op=[0.492 0.498 0.503 0.509 0.514 0.52 0.524 0.528 0.532 0.533 0.53];
ASE=[0.0499 0.0509 0.0517 0.0528 0.0539 0.0553 0.057 0.0594 0.0629 0.0694 0.0826];
SNR=[19.72 19.57 19.46 19.28 19.07 18.81 18.39 17.78 16.92 15.36 12.83];
x=[0:0.01:100]; y1=interp1(L,Op,x,'spline'); y2=interp1(L,ASE,x,'spline'); y3=interp1(L,SNR,x,'spline');
figure(1)
n=4;
p=polyfit(x,y1,n);
yi=polyval(p,x);
[M,h1,h2]=plotyy(x,yi,x,y2);
set(gca,'linewidth',2);
box off
set(h1,'linewidth',2);
set(h1,'linestyle','-');
set(h2,'linewidth',2);
xlabel('Forward pump beam (% of total pump energy)','FontSize',13);
ylabel(M(1),'Output Seedbeam Power (W)','FontSize',13);
ylabel(M(2),'ASE Power with output seed beam(W)','FontSize',13);
set(M(1),'ytick',[0.49:0.01:0.55],'ylim',[0.49 0.55]);
set(M(2),'ytick',[0.04:0.01:0.1],'linewidth',2)
hh=legend('Output seedbeam ','ASE');
set(hh,'FontSize',13)
hold on
[M,h1,h2]=plotyy(L,Op,L,ASE);
set(h1,'linestyle','o');
set(h2,'linestyle','x');
set(M(1),'ytick',[0.49:0.01:0.55],'ylim',[0.49 0.55]);
set(M(2),'ytick',[0.04:0.01:0.1],'linewidth',2)
hold off
My problem is that ASE is not being properly plotted against L in the right y-axis, Op plots just fine. When I try to plot ASE the axis scaling gets thrown off even though I explicitly state that I want it to remain identical to the initial plot. Just to recap, I need Op and yi plotted using the left y-axis, and I need ASE and y2 plotted against the right y-axis, without the scale getting ruined in the process. I feel like I am so close to finishing this assignment. Thanks for your help!
Derek

回答(2 个)

Fangjun Jiang
Fangjun Jiang 2011-6-24
I am not sure what is the problem. If you want to adjust the scale, can't you just use the axis() function?

Arturo Moncada-Torres
I think in a raw way, this is what you want, am I right?
L=[0 10 20 30 40 50 60 70 80 90 100];
Op=[0.492 0.498 0.503 0.509 0.514 0.52 0.524 0.528 0.532 0.533 0.53];
ASE=[0.0499 0.0509 0.0517 0.0528 0.0539 0.0553 0.057 0.0594 0.0629 0.0694 0.0826];
SNR=[19.72 19.57 19.46 19.28 19.07 18.81 18.39 17.78 16.92 15.36 12.83];
x=[0:0.01:100];
y1=interp1(L,Op,x,'spline');
y2=interp1(L,ASE,x,'spline');
y3=interp1(L,SNR,x,'spline');
n=4;
p=polyfit(x,y1,n);
yi=polyval(p,x);
figure();
hold('on');
plot(L,Op,'ro');
plot(L, ASE, 'rx');
plot(x, yi, 'b');
plot(x, y2, 'g');
hold('off');
EDIT Notice how the new plot has the two original lines (in blue and green) and the points ("o" and "x") in red. The x axis goes from 0 to 100 and the y axis is the same for all of the plots.
Run it and let me know.
  3 个评论
Arturo Moncada-Torres
Yikes, see the edit, I forgot to delete one line of code to produce only the plot you want.
Derek
Derek 2011-6-24
Im sorry Arturo that still does not work. I want a plot with 2 y-axes, and the x-axis going from 0 to 100 in increments of ten. the first y-axis (left one) should have the range 0.49:0.01:0.55 while the right hand one should have 12:1:20. ASE and y2 need to be plotted wrt the right axis, and Op and yi plotted wrt the left axis.
Your edit has ASE (only points) and Op (also only points) as I need, as well as best fit curves (yi and y2, as I need) however they are all plotted against the left y-axis. I am currently trying to tweak your code to use the plotyy function.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Log Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by