Legend color not correspondante to plot for a Plot with loop
1 次查看(过去 30 天)
显示 更早的评论
Dear all, even using 2016a version i have a problem in legend color which are not correspondante to plot for a plot with loop, this is my code
V_Time = [1, 2, 3]; % Time Vector fixe length
%
V_Data = 1:1:12; % Data Vector
Nbr_Position = 2; % Nbr of position
Nbr_Case = 2; % Nbr of cases
%
N_Rows= length(V_Time); % Nbr of rows for matrix
N_columns= length(V_Data)/N_Rows; % Nbr of columns for matrix
M_Brut= (reshape(V_Data,[N_Rows,N_columns])); % Brut matrix from Data Vector
%
%
for i=1:Nbr_Position % loop by position
M_Nett{i} = M_Brut(:,(i):(Nbr_Position):(end)); % Nett matrix
figure
for j=1:Nbr_Case % loop by case
plot(V_Time',M_Nett{i}(:,1:j)) % plot pair
hold on
end
title(['Position N°: ' num2str(i)]) % title
legend(genvarname(repmat({'Case'},1,Nbr_Case),'Case')) % legend
end
This is the result plot
0 个评论
回答(1 个)
reen2015
2016-4-11
Hi
it must be because the color is changing per iteration as you didnt specify any color in plot. Try specifying color like plot(x,t,'b') for each case separately, so as to make sure the same color appears in legend.
Good luck
Raina
3 个评论
reen2015
2016-4-11
编辑:reen2015
2016-4-11
I did this
clc;clear all;close all;
V_Time = [1, 2, 3]; % Time Vector fixe length
%
V_Data = 1:1:12; % Data Vector
Nbr_Position = 2; % Nbr of position
Nbr_Case = 2; % Nbr of cases
%
N_Rows= length(V_Time); % Nbr of rows for matrix
N_columns= length(V_Data)/N_Rows; % Nbr of columns for matrix
M_Brut= (reshape(V_Data,[N_Rows,N_columns])); % Brut matrix from Data Vector
%
%
for i=1:Nbr_Position % loop by position
M_Nett{i} = M_Brut(:,(i):(Nbr_Position):(end)); % Nett matrix
figure
for j=1:Nbr_Case % loop by case
if i==1
plot(V_Time',M_Nett{i}(:,1:j),'c') % plot pair
hold on
end
if i==2
plot(V_Time',M_Nett{i}(:,1:j),'r') % plot pair
hold on
end
end
title(['Position N°: ' num2str(i)]) % title
legend(genvarname(repmat({'Case'},1,Nbr_Case),'Case')) % legend
end
But I could not separate cases as i couldnt figure out your cases.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!