How can I change legend colors if I have multiple graphs of the same color?

2 Ansichten (letzte 30 Tage)
I'm plotting 10 funtcions in the same graph, 5 of one type in red and 5 of another type in blue.
I would like to create a legend where there is a red line that says "Type1" and a blue line that says "Type2".
I didn't find a way to do that since the lengend is expecting 10 names, and if I put only two of them, those are both red.

Akzeptierte Antwort

Sachin Shrestha
Sachin Shrestha am 11 Jun. 2016
Hi Leonardo,
You may specify the handles of the plots for which you want to have the legend. In your case, you may specify handles, one for red line and another for blue line and generate legends for those only. For instance:
handle1=plot(1:10,'r'); % define handle for one of the red lines
hold on;
plot(1:2:20,'r');
plot(1:3:30,'r');
plot(1:4:40,'r');
plot(1:5:50,'r');
handle2=plot(3:10,'b'); % define handle for one of the blue lines
plot(3:2:20,'b');
plot(3:3:30,'b');
plot(3:4:40,'b');
plot(3:5:50,'b');
legend([handle1 handle2],{'type1','type2'}); % define legend for plots related to handles
Hope this helps. Good Luck!

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by