# legend doesn't list all of the plotted lines using plotyy

2 views (last 30 days)
Leo Simon on 27 Oct 2020
Commented: Leo Simon on 27 Oct 2020
In the code below, 8 lines are plotted, but the legend displays only 7 of them. Could somebody please explain how I can display all eight lines in the legend? Thanks very much for any suggestions
YY{1,1} = [ -0.1479019945775 -0.1207614728849 -0.0853912563830
-0.1332722959302 -0.1088113389903 -0.0769390531180
-0.1204885338805 -0.0983676376342 -0.0695513925373];
YY{2,1} = [ -0.1479019945775 -0.1207614728849 -0.0853912563830
-0.1315463790731 -0.1074015009182 -0.0759418727129
-0.1170135908441 -0.0955291160389 -0.0675440123853];
YY{1,2} = [ 1.8095706646177
1.6688286822702
1.5392601779117];
YY{2,2} = [ 1.8095706646177
1.6517555878194
1.5033917356162];
close all;
hold on;
[hPlotyy{1},plotyyAx{1,1},plotyyAx{2,1}]=plotyy([1:3],YY{1,1},[1:3],YY{1,2})
[hPlotyy{2},plotyyAx{1,2},plotyyAx{2,2}]=plotyy([1:3],YY{2,1},[1:3],YY{2,2});
legend

Cris LaPierre on 27 Oct 2020
I think the issue here is that plotyy is not designed to work with hold on. It creates 2 axes superimposed on itself. When called twice as you do, it creates 3 axes.
Heed the warning on the plotyy documentation page and use yyaxis instead.
YY{1,1} = [ -0.1479019945775 -0.1207614728849 -0.0853912563830
-0.1332722959302 -0.1088113389903 -0.0769390531180
-0.1204885338805 -0.0983676376342 -0.0695513925373];
YY{2,1} = [ -0.1479019945775 -0.1207614728849 -0.0853912563830
-0.1315463790731 -0.1074015009182 -0.0759418727129
-0.1170135908441 -0.0955291160389 -0.0675440123853];
YY{1,2} = [ 1.8095706646177
1.6688286822702
1.5392601779117];
YY{2,2} = [ 1.8095706646177
1.6517555878194
1.5033917356162];
% Create figure using yyaxis
yyaxis left
plot([1:3],YY{1,1},[1:3],YY{2,1});
yyaxis right
plot([1:3],YY{1,2},[1:3],YY{2,2});
legend
Leo Simon on 27 Oct 2020
Perfect, thanks very much