legend with multiple axes using yyaxis

I am creating a plot with two different y axes. the curves are added alternately. for example:
yyaxis left
plot(A)
yyaxis right
plot(B)
yyaxis left
plot(C)
yyaxis right
plot(D)
My problem is that when I add a legend to the plot, the order of the curves is not the order in which they were created. instead, they are ordered first by the left axis
and then by the right axis. meaning if i write down "legend(A,B,C,D)" the legend entry B will correspond with the curve C and vice-versa. is there a way to fix this? The actual case there are more than 4 curves, and the number changes every time, so manual changing is not possible. many thanks in advance
Nathan

 採用された回答

Star Strider
Star Strider 2021 年 1 月 2 日
編集済み: Star Strider 2021 年 1 月 2 日

6 投票

Try this:
A = rand(1,10);
B = rand(1,15);
C = rand(1,20);
D = rand(1,30);
figure
hold on
yyaxis left
ha = plot(A);
yyaxis right
hb = plot(B);
yyaxis left
hc = plot(C);
yyaxis right
hd = plot(D);
hold off
legend([ha,hb,hc,hd], 'A','B','C','D')
Alternatively:
figure
yyaxis left
ha = plot(A);
hold on
hc = plot(C);
hold off
yyaxis right
hb = plot(B);
hold on
hd = plot(D);
hold off
legend([ha,hb,hc,hd], 'A','B','C','D')
Using hold is the only way to do what you want. This cannot be used with your posted code, because without the hold function, the second calls to yyaxis left and yyaxis right calls eliminate the handles to the previously plotted curves.
EDIT — (2 Jan 2021 at 17:41)
Added alternative code.
.

6 件のコメント

Nathan Blanc
Nathan Blanc 2021 年 1 月 2 日
Thank you Star. I didn't quite understand why you kept holding on and off in your code, but the naming of the plots and addition to the legend command solved my problem
Nathan
Star Strider
Star Strider 2021 年 1 月 2 日
As always, my pleasure!
The hold function permits subsequent plots on the same axes, and that is necessary here. (I thought that I already posted a link to that function, however I now realise that did not, and have now corrected that. My apologies for the oversight.)
Giovanni Bambini
Giovanni Bambini 2023 年 2 月 2 日
編集済み: Giovanni Bambini 2023 年 2 月 2 日
This does not seem to work when grouping bars:
A = [rand(10,1) zeros(10,1)];
B = [zeros(10,1) rand(10,1)];
figure;
yyaxis left
ha = bar(A, 'FaceColor', '#0000FF');
set(findobj(gcf,'type','axes'), 'ycolor', '#0000FF');
yyaxis right
hb = bar(B, 'FaceColor', '#00BCFF');
set(findobj(gcf,'type','axes'), 'ycolor', '#00BCFF');
legend([ha hb], 'h1', 'h2');
Warning: Ignoring extra legend entries.
Please help
E.
Voss
Voss 2023 年 2 月 2 日
ha and hb are each 2 bar objects, as shown below. Therefore, [ha hb] is four bar objects. You can use just the first element of each ha and hb for the legend, [ha(1) hb(1)]
A = [rand(10,1) zeros(10,1)];
B = [zeros(10,1) rand(10,1)];
figure;
yyaxis left
ha = bar(A, 'FaceColor', '#0000FF')
ha =
1×2 Bar array: Bar Bar
set(findobj(gcf,'type','axes'), 'ycolor', '#0000FF');
yyaxis right
hb = bar(B, 'FaceColor', '#00BCFF')
hb =
1×2 Bar array: Bar Bar
set(findobj(gcf,'type','axes'), 'ycolor', '#00BCFF');
legend([ha(1) hb(1)], 'h1', 'h2');
Giovanni Bambini
Giovanni Bambini 2023 年 2 月 3 日
Thanks!!!!
Star Strider
Star Strider 2023 年 2 月 3 日
My pleasure!
A Vote would be appreciated!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Object Properties についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by