how to use 2 plotyy to generate a figure with four lines

1 view (last 30 days)
I want to generate a figure with four lines, each 2 are created by plotyy.
But the figure generated always shows a wrong y axis position. code attached:
f=fittype('a/(b*x^(-1)+1)','coefficients',{'a','b'});
vfun=fit(r12,v12_20,f);
x=100:1:2000;
v=(vfun(x))';
% fit Power
p=1000*(v.^2./x);
% plot the original %
[AX,H1,H2]= plotyy(r12,v12_20,r12,p12_20);
set(AX(1),'YLim',[0.5 2]);
set(AX(1),'YTick',[0.5:0.5:2]);
set(AX(2),'YLim',[0 2]);
set(AX(2),'YTick',[0:0.5:2]);
set(H1,'linewidth',3);
set(H2,'linewidth',3);
axes(AX(1));
ylabel('Voltage (mV)','fontsize',30);
set(gca,'fontsize',30,'linewidth',3);
axes(AX(2));
ylabel('Power (nW)','fontsize',30);
xlabel('Load Resistance (\Omega)','fontsize',30);
set(gca,'fontsize',30,'linewidth',3);
hold on;
% plot
[AX,H3,H4]=plotyy(x,v,x,p);
set(AX(1),'YLim',[0.5 2]);
set(AX(1),'YTick',[0.5:0.5:2]);
set(AX(2),'YLim',[0 2]);
set(AX(2),'YTick',[0:0.5:2]);
set(H3,'linewidth',3);
set(H4,'linewidth',3);
axes(AX(1));
ylabel('Voltage (mV)','fontsize',30);
set(gca,'fontsize',30,'linewidth',3);
axes(AX(2));
ylabel('Power (nW)','fontsize',30);
xlabel('Load Resistance (\Omega)','fontsize',30);
set(gca,'fontsize',30,'linewidth',3);
grid on;
Hope I can have some suggestions from here. Many thanks.
  1 Comment
dpb
dpb on 22 Sep 2014
Edited: dpb on 22 Sep 2014
Undefined function or variable 'r12'.
So can't try to duplicate your case...I suggest
a) cut it down to the barest minimum w/ random data or some other dataset that reproduces the problem--it shouldnt need any of the niceties to see the axis scaling issues, but we can't see what happens as is, and
b) Explain more clearly what it is you see and what it is you're expecting.
My first reaction is why two call to plotyy? Why don't you simply put the two x and y vectors in two columns as
[AX,H1,H2]= plotyy([r12 x],[v12_20 v],[r12 x],[p12_20 p]);
???
BTW, I didn't read the original closely enough to tell, but the above assumes the same length of vectors for the two sets of variables. If that's not the case, still can use the form, simply augment the shorter of the two with NaN to length of the longer and plotyy will happily ignore the extra data.
ADDENDUM
One mistake that quite possible is tied into your problem is
[AX,H1,H2]= plotyy(r12,v12_20,r12,p12_20);
...
[AX,H3,H4]=plotyy(x,v,x,p);
You've wiped out the saved axes handles AX in the second call so you've no longer got any easy access to them. Simply destroying the variable in the workspace shouldn't have had any effect on the display, but you've certainly made it a lot more difficult to fix up anything after having done so. Use two separate arrays as you did for the other object handles or append the second to to the same array, your choice, but be sure to keep them all if you're adamant in trying to do it this way.

Sign in to comment.

Answers (1)

Chad Greene
Chad Greene on 22 Sep 2014
There are a number of solutions on FEX that might help.

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!