How do i select data to legend in a plot ?

27 次查看(过去 30 天)
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) ?

采纳的回答

Mischa Kim
Mischa Kim 2014-5-14
编辑:Mischa Kim 2014-5-14
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by