Color of legend does't change in the loop.

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

采纳的回答

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