필터 지우기
필터 지우기

Color of legend does't change in the loop.

조회 수: 1 (최근 30일)
Sa Moha
Sa Moha 2021년 12월 10일
답변: Rik 2021년 12월 10일
Hi guys
Please run this simple code:
syms x z;
for i=1:2
z=[x,0];
fplot(z(1,1),[i i+1] , 'r');
title('z(x)');
hold on;
end
for j=1:2
z=[-x,0];
fplot(z(1,1),[j j+1] , 'b');
title('z(x)');
legend('one' , 'two');
hold on;
end
Why the legend color is red for 'two' ?
I defined blue for it. Can someone help here please?
Thank you.
  댓글 수: 1
KSSV
KSSV 2021년 12월 10일
It is because, the first loop plots in red color.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2021년 12월 10일
Each call to fplot will create a line object. So in your code you create 4 objects, but you provide only 2 legend entries.
syms x z;
plot_handles=[];
for i=1:2
z=[x,0];
plot_handles(i)=fplot(z(1,1),[i i+1] , 'r');
hold on;
end
for j=1:2
z=[-x,0];
plot_handles(i+j)=fplot(z(1,1),[j j+1] , 'b');
hold on;
end
title('z(x)');
legend(plot_handles([1 3]),{'one' , 'two'});

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by