How to show multiple lines in the legend?

7 ビュー (過去 30 日間)
Wiqas Ahmad
Wiqas Ahmad 2021 年 4 月 12 日
回答済み: Cris LaPierre 2021 年 4 月 12 日
The legend shows only one value of EC which is the last one as shown in fig, however my EC has 10 values and I'd lke to display all of them in the legend one-byone in a column. The EC vector and syntax for the lenegd I'm using can be seen
EC = [0.0052 0.0078 0.0104 0.0130 0.0156 0.0182 0.0208 0.0234 0.0260 0.0286];
...
for j = 1:length(EC)
...
lgd=legend(['EC=',num2str(EC(j)),'m^{-1}']);
...
end

回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 4 月 12 日
You are replacing the legend each time. Use the loop to build up your labels, but don't atually create the legend until you exit the for loop.
Another option is to use the 'DisplayName' name value pair in your plot command.
EC = [0.0052 0.0078 0.0104 0.0130 0.0156 0.0182 0.0208 0.0234 0.0260 0.0286];
for j = 1:length(EC)
plot(rand(1,5),'DisplayName',['EC=',num2str(EC(j)),'m^{-1}']);
hold on
end
hold off
legend
I'm not sure you want to include a lenged label for every line in your plot. I don't see how that will clarify anything.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by