Legend color not correspondante to plot for a Plot with loop

1 回表示 (過去 30 日間)
Ferial Assmani
Ferial Assmani 2016 年 4 月 11 日
編集済み: Ferial Assmani 2016 年 4 月 11 日
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

回答 (1 件)

reen2015
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
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.
Ferial Assmani
Ferial Assmani 2016 年 4 月 11 日
編集済み: Ferial Assmani 2016 年 4 月 11 日
But all legend have the same color ? How distinguished each one ?

サインインしてコメントする。

カテゴリ

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