Question

I have following simple example to display line plot using different color for each class with legend.

A = [
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2
11 12 13 14
15 16 17 18
19 18 17 16
15 14 13 12
    ];
B = [1 1 1 1 2 2 2 2    ];
p1=plot(A(B==1,:)','r');
hold on
p2=plot(A(B==2,:)','b');
legend([p1;p2],'Red lines','Blue lines');

But the script produced a warning message saying

Warning: Ignoring extra legend entries. 
> In legend at 291 

And two legends displayed with same color of lines. What's going on?

enter image description here

Was it helpful?

Solution

p1 and p2 each contains a multiplicity of handles, therefore you need to select one of each (since you have provided only two labels):

legend([p1(1) p2(1)],'Red lines','Blue lines');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top