![Screen Shot 2018-12-25 at 11.32.24 PM.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/199739/Screen%20Shot%202018-12-25%20at%2011.32.24%20PM.png)
Only last legend command is working while using multiple legend command using plotyy
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Subhamoy Saha
il 25 Dic 2018
Commentato: Subhamoy Saha
il 26 Dic 2018
My code looks like this:
n=-180:180;
x=(pi/180).*n;
y=sin(x);
y2=sin(x).*cos(x);
y3=cos(x);
[ax h1 h2]=plotyy(n,y,n,y2)
h3=line(n,y3,'Color','r','LineWidth',2,'Parent',ax(1))
set(h1,'Color','k','LineStyle','-','LineWidth',1.0);
set(h2,'Color','b','LineStyle','-','LineWidth',1.5);
legend(ax(1),'h1','h3','Location','NorthEast')
legend(ax(2),{'h2'},'Location','NorthWest');
the last legend command is working
![1.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/199736/1.jpeg)
n=-180:180;
x=(pi/180).*n;
y=sin(x);
y2=sin(x).*cos(x);
y3=cos(x);
[ax h1 h2]=plotyy(n,y,n,y2)
h3=line(n,y3,'Color','r','LineWidth',2,'Parent',ax(1))
set(h1,'Color','k','LineStyle','-','LineWidth',1.0);
set(h2,'Color','b','LineStyle','-','LineWidth',1.5);
legend(ax(1),'h1','h3','Location','NorthEast')
% legend(ax(2),{'h2'},'Location','NorthWest');
but after disabling the last legend command
![2.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/199737/2.jpeg)
Please help how to keep both the legend boxes.
0 Commenti
Risposta accettata
madhan ravi
il 25 Dic 2018
Use yyaxis:
n=-180:180;
x=(pi/180).*n;
y=sin(x);
y2=sin(x).*cos(x);
y3=cos(x);
yyaxis left
a=plot(n,y,'k')
legend('A')
yyaxis right
aa=plot(n,y2,'b')
hold on
aaa=plot(n,y3,'r')
legend([a aa aaa],{'A','B','C'})
![Screen Shot 2018-12-25 at 11.32.24 PM.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/199739/Screen%20Shot%202018-12-25%20at%2011.32.24%20PM.png)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Two y-axis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!