Legend with certain number of rows and columns

43 ビュー (過去 30 日間)
dj1du
dj1du 2023 年 1 月 25 日
コメント済み: Davide Masiello 2023 年 1 月 25 日
Hello everyone,
I would like to plot 6 curves in a diagram and the legend should have the first 4 curves in the first column and the last two curves in the second column. How can I arrange this? Thank you very much for your help!
  1 件のコメント
Askic V
Askic V 2023 年 1 月 25 日
Davide provided the answer. Just in case you're interested to learn more about different options, similar question was answered by Mathworks Support Team here:
https://www.mathworks.com/matlabcentral/answers/337756-how-can-i-make-a-legend-that-has-both-rows-and-columns

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

採用された回答

Davide Masiello
Davide Masiello 2023 年 1 月 25 日
編集済み: Davide Masiello 2023 年 1 月 25 日
Use legendflex from file exchange.
x = linspace(0,1,1000);
n = (1:6)';
y = x.^n;
p = plot(x,y);
title('y = x^n')
legendflex(p,{'n=1','n=2','n=3','n=4','n=5','n=6'},...
'ncol',2,'nrow',4,'anchor',[1 1],'buffer',[10 -10],'box','off')
  2 件のコメント
dj1du
dj1du 2023 年 1 月 25 日
Thank you very much, that looks very good! But I created the six curves by different plot commands, i.e. according to your naming I would introduce variables p1,p2,p3,p4,p5,p6 for each plot. What would be the correct syntax in this case when calling legendflex?
Davide Masiello
Davide Masiello 2023 年 1 月 25 日
I guess this could be considered cheating, but it does work
x = linspace(0,1,1000);
n = (1:6)';
y = x.^n;
p1 = plot(x,y(1,:)); hold on
p2 = plot(x,y(2,:));
p3 = plot(x,y(3,:));
p4 = plot(x,y(4,:));
p5 = plot(x,y(5,:));
p6 = plot(x,y(6,:));
title('y=x^n')
legendflex(p1,{'n=1'},'anchor',[1 1],'buffer',[10 -10],'box','off')
legendflex(p2,{'n=2'},'anchor',[1 1],'buffer',[10 -25],'box','off')
legendflex(p3,{'n=3'},'anchor',[1 1],'buffer',[10 -40],'box','off')
legendflex(p4,{'n=4'},'anchor',[1 1],'buffer',[10 -55],'box','off')
legendflex(p5,{'n=5'},'anchor',[1 1],'buffer',[80 -10],'box','off')
legendflex(p6,{'n=6'},'anchor',[1 1],'buffer',[80 -25],'box','off')

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by