Erratic behavior of Legend function
조회 수: 1 (최근 30일)
이전 댓글 표시
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.

댓글 수: 0
답변 (1개)
Geoff Hayes
2015년 2월 15일
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?
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!