How do i select data to legend in a plot ?

144 views (last 30 days)
Hello,
I didn't find the way to legend the data as i want. A little code to explain :
data1=10*ones(10,4)+randn(10,4);
t1=(0:1:9);
figure();
plot(t1,data1,'bo');
hold on
data2=25*ones(100,4)+2*randn(100,4);
t2=(0:0.1:9.9);
plot(t2,data2,'rx');
legend([{'From data1'},{'From data2'}]);
And I want to create a legend which say "Blue circle are from the data1" and "Red cross from the data2", in two lines.
Here, both text refers to blue circle - data1 -, i think it's normal since there is four plot of data1. If a put four {'From data1'} cells, the last one is a red cross, but i don't want to have a big legend.
Does anybody know how to display only one label per group (with good marks) ?

Accepted Answer

Mischa Kim
Mischa Kim on 14 May 2014
Edited: Mischa Kim on 14 May 2014
Blaise, use
data1 = 10*ones(10,4)+randn(10,4);
t1 = (0:1:9);
figure();
p1 = plot(t1,data1,'bo');
hold on
data2 = 25*ones(100,4)+2*randn(100,4);
t2 = (0:0.1:9.9);
p2 = plot(t2,data2,'rx');
legend([p1(1),p2(1)],'From data1','From data2');
The issue here is that the first plot already contains four data sets. With two legend entries you only get a legend for the first two data sets in the first plot, which is why they are both marked the same, with a blue circle.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!