Effacer les filtres
Effacer les filtres

How to show multiple lines in the legend?

17 vues (au cours des 30 derniers jours)
Wiqas Ahmad
Wiqas Ahmad le 12 Avr 2021
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

Réponses (1)

Cris LaPierre
Cris LaPierre le 12 Avr 2021
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