2D plot help

3 views (last 30 days)
Derek
Derek on 23 Jun 2011
[EDIT: 20110623 10:21 CDT - reformat - WDR]
Hi everybody. I have been given the following code,
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);
plot(x,yi,'-');
[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)
I have used the plotyy function to plot yi (which is the best fit curve of y1) as well as y2 against x. I need to add ASE and Op to my plot as well (versus x) to essentially have 2 sets of data points, and 2 curves on the same plot. I have tried adding them to the plotyy function to no avail and am currently searching around for another way to do it.
Any help would be appreciated.
Thanks!

Answers (1)

Arturo Moncada-Torres
Arturo Moncada-Torres on 23 Jun 2011
You could overlap plots using hold. For example:
figure();
hold('on');
plot(1:10, 'b*');
plot(10:-1:1, 'ro');
hold('off');
However, the problem here is that the x scale for your plotted data does not match with the scale for ASE or Op, so even if you put them together the visualization would not be correct. Try fixing that and then include the hold part in your code.
  2 Comments
Derek
Derek on 23 Jun 2011
Thanks Arturo you have helped me get on the right track. I added this piece of code to the bottom,
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
and everything is appearing on my plot. However my right axis scale is getting thrown off even though I explicitly tell it to be the same as the original.
Is there a way to use the simple plot function (not plotyy) but to plot against the right axis?
Arturo Moncada-Torres
Arturo Moncada-Torres on 23 Jun 2011
Since you got an answer to your original question, could you please mark this question as answered and ask this in a new thread? This is to keep the forum as clean as possible. I will continue helping you there ;) !

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!