Legend does not have the right color every two plots.

조회 수: 2 (최근 30일)
Leandro Silva
Leandro Silva 2020년 10월 17일
댓글: Leandro Silva 2020년 10월 17일
%Every even plot inside the "for" has a mismatch in color(and the mismatches have the same color).
%I'm plotting from simulink. Tried fixing using the vector "cor" inthe plot but didn't help.
clear all
close all
clc
legend('off');
legend('show');
Vref=0.1;
gamma=0.1;
Kd=1:1:6;
Kp=1:1:6;
Ki=1:1:6;
cor=[[1 1 0]; [1 0 1]; [0 1 1]; [1 0 0]; [0 1 0]; [0 0 1]; [0 0 0]]
for i = 1:size(Kd,2)
K_d=Kd(i);
for j= 1:size(Kp,2)
K_p=Kp(j);
info=sim('diagrama_blocos');
Vt_out=info.Vt.signals.values(:,1);
Vt_ref_out=info.Vt.signals.values(:,2);
time_out=info.Vt.time;
plot(time_out,Vt_out,'LineWidth',2,'Color', cor(j,:));grid on;hold all;
line([time_out(1),time_out(end)],[Vref,Vref]);
legendInfo{j}=['Kp= ', num2str(K_p)];
title(['Curvas para Kd= ', num2str(K_d)]);
end
legend(legendInfo)
figure()
end

채택된 답변

Adam Danz
Adam Danz 2020년 10월 17일
"Legend does not have the right color every two plots"
That's because you're adding two lines on each iteration of your loop.
for i = 1:size(Kd,2)
K_d=Kd(i);
for j= 1:size(Kp,2)
K_p=Kp(j);
info=sim('diagrama_blocos');
Vt_out=info.Vt.signals.values(:,1);
Vt_ref_out=info.Vt.signals.values(:,2);
time_out=info.Vt.time;
% LINE 1
plot(time_out,Vt_out,'LineWidth',2,'Color', cor(j,:));grid on;hold all;
% LINE 2
line([time_out(1),time_out(end)],[Vref,Vref]);
legendInfo{j}=['Kp= ', num2str(K_p)];
title(['Curvas para Kd= ', num2str(K_d)]);
end
legend(legendInfo)
figure()
end
Instead of including any and all objects on your legend, and instead of storing a list of legend strings separately from their corresponding objects, use this 2-step method defined in this answer. The result will look something like this (see inline comments for details,
% Don't do this
% clear all
% close all
% clc
% legend('off');
% legend('show');
Vref=0.1;
gamma=0.1;
Kd=1:1:6;
Kp=1:1:6;
Ki=1:1:6;
cor=[[1 1 0]; [1 0 1]; [0 1 1]; [1 0 0]; [0 1 0]; [0 0 1]; [0 0 0]]
for i = 1:size(Kd,2)
K_d=Kd(i);
% Create figure here
figure()
% Set this stuff prior to the j-loop
grid on
hold on
% Preallocate the plot handles
plotHandles = gobjects(1,size(Kp,2));
for j= 1:size(Kp,2)
K_p=Kp(j);
info=sim('diagrama_blocos');
Vt_out=info.Vt.signals.values(:,1);
Vt_ref_out=info.Vt.signals.values(:,2);
time_out=info.Vt.time;
% 1) Store output handles and 2)Define DisplayName propery
plotHandles(j) = plot(time_out,Vt_out,'LineWidth',2,'Color', cor(j,:), ...
'DisplayName', ['Kp= ', num2str(K_p)]);
% Move this out of the j-loop
%grid on;hold all;
line([time_out(1),time_out(end)],[Vref,Vref]);
% No need for this anymore
% legendInfo{j}=['Kp= ', num2str(K_p)];
% Move this outside of the loop
% title(['Curvas para Kd= ', num2str(K_d)]);
end
% Set title
title(['Curvas para Kd= ', num2str(K_d)]);
% Call legend with list of handles
legend(plotHandles)
% Move this to before the j-loop
% figure()
end
  댓글 수: 1
Leandro Silva
Leandro Silva 2020년 10월 17일
Thank you so much for the help and for comments in the code!

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

추가 답변 (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