How to use legend in two for loops

2 views (last 30 days)
Naga A
Naga A on 29 Nov 2015
Answered: Adithya Addanki on 1 Dec 2015
Hello all,
In my program ,I'm using two loops like this:
lambda=[1 2 3 4 5];
alpha=[2 4 6 8];
for alphaloop=1:10
for lambdaloop=1:10
plot(xx,yy,'color',color_value(lamdaloop));
legend({'\lambda=1','\lambda=2','\lambda=3','\lambda=4','\lambda=5'});
hold on
end
legend({'\alpha=2','\alpha=4','\alpha=6','\alpha=8'});
end
I'm using different colors for each lambda value and different styles for each alpha value. But it displaying only alpha value at the end. I want to display what color is valued for lambda and what style is used for alpha.Thanks

Answers (1)

Adithya Addanki
Adithya Addanki on 1 Dec 2015
Hi Naga,
In my understanding you want to display two legends on the single plot, where the combination of the two legends are used to explain the characteristics of the plot. In your case: color(lambda) and style (alpha).
Please know that there is no direct way available to do this. Our development team has been informed about this usecase. It may be considered for a future release of MATLAB.
As a workaround, you will need to have two plots per line (with each plot using one property) if you need two legends. Then add the legend for the first property by only using the lines that are representative of the first property. You can then use the 'copyobj' function to copy the legend and its axis.
Then in the new axis you can change the legend in that handle to be the legend of only those lines that are representative of the second property. Make sure that both the legends are outside the plot. You can make one of the axis invisible if needed.
Please refer to the attached script for an example.
I hope this helps.
Thank you,
Adithya Addanki

Community Treasure Hunt

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

Start Hunting!