Legend does not have the right color every two plots.

2 views (last 30 days)
%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

Accepted Answer

Adam Danz
Adam Danz on 17 Oct 2020
"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

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!