How do I make a figure legend multiple lines?
386 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2011 年 1 月 13 日
回答済み: Sulaymon Eshkabilov
2022 年 5 月 18 日
The string for my legend is too long to fit into one line. I would like to split it into multiple lines.
採用された回答
MathWorks Support Team
2020 年 6 月 9 日
編集済み: MathWorks Support Team
2020 年 11 月 23 日
To split legend to multiple lines, use the "newline" function or the ASCII character 10 for new line, for example:
>> plot(sin(1:10),'b')
>> hold on
>> plot(cos(1:10),'r')
>> legend({['blue' char(10) 'line'],'red line'})
With MATLAB "strings" you can use the following notation:
>> legend({ "blue" + newline + "line", 'red line'})
or
>> legend({ strcat("blue", string(newline), "line"), 'red line' })
In addition to the MATLAB command line, the legend can also be updated interactively in the figure itself to include multi-line legend entries.
0 件のコメント
その他の回答 (2 件)
Christoforos Kanavakis
2018 年 3 月 23 日
In newer versions, Matlab recommends using 'newline'.
So,
legend({['blue' newline 'line'],'red line'})
0 件のコメント
Sulaymon Eshkabilov
2022 年 5 月 18 日
This one is a better solution that is easier to implement:
plot(sin(1:.1:10),'r', 'DisplayName', 'Cosine'), hold on
plot(cos(1:.1:10),'g', 'DisplayName', 'Sine')
plot(tan(1:.1:10),'b', 'DisplayName', 'Tangent')
plot(1./tan(1:.1:10),'k', 'DisplayName', 'Inverse Tangent'), grid on
My_LGD = legend;
My_LGD.NumColumns = 2; % Show legends in two lines
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!