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 Center および File ExchangeLegend についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by