Erratic behavior of Legend function

2 views (last 30 days)
Priyanka
Priyanka on 15 Feb 2015
Answered: Geoff Hayes on 15 Feb 2015
0 down vote favorite
I was working on a matlab project and found strange behavior of legend function. I am posting similar code to reproduce the issue (Kindly ignore the weird looking plot). below is the code
X=rand(3,100);
a=rand(3,100);
b=rand(3,100);
figure(1); hold on;
plot(0.17663,'m');
plot(1.223,'y');
plot(X,a,'r');
plot(X,b,'g');
legend({'plot1','plot2','plot3','plot4'});
so my question is the legend in the picture shows the same color for plot 3 and plot4. Instead it should show red and green respectively. Please help me to sort this out. Thanks.

Answers (1)

Geoff Hayes
Geoff Hayes on 15 Feb 2015
Priyanka - when you plot X vs a as
plot(X,a,'r');
remember that X and a are 3x100 matrices and so you are in fact plotting 100 curves each with their own handles. So if you were to do
h = plot(X,a,'r');
h would be a 100x1 array and all 100 curves/lines would be drawn in red. When you call
legend({'plot1','plot2','plot3','plot4'});
plot3 and plot4 correspond to two of the 100 curves drawn in red...which explains your observation.
What does your X and a data correspond to? Could they be reshaped to 1x300 arrays instead?

Tags

Community Treasure Hunt

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

Start Hunting!