How to decide the color on the legend

Hi guys! I want to plot 2 lines at each time (for 5 times) on the same plot, so I want to choose the color. I wrote the code in this way:
N = 5;
col=hsv(N); %colors figure
for i=1:N
mtot_h = 2000+i*500-500; % [kg/h]
mtot = mtot_h/3600; % [kg/s]
minlet = [2/3*mtot 0 0 0 0 1/3*mtot]; % inlet mass flow rates [kg/s]
Fi0 = minlet./MWi; % [mol/s]
inlet = [Fi0 Tin TweFirstGuess Pin];
M = eye(9); M(8,8)=0;
options = odeset('Mass',M,'RelTol',1E-8,'AbsTol',1E-12);
[z,outlet] = ode15s(@PFR,[0 L],inlet,options);
FC3H8 = outlet(:,1); % [mol/s]
...
figure(1)
hold on
plot(z,T-273.15,z,Twe-273.15,'Color',col(i,:))
refline(0,Tmax) % horizzontal line at T=1100°C
xlabel 'Reactor length [m]', ylabel 'Temperature [°C]', title 'Temperature profiles'
legendInfo{i} = ['Mtot: ' num2str(mtot_h) ' kg/h'];
legendInfo{i+1} = 'T max';
legend(legendInfo,'Location','southeast')
end
With the command "color" inside the plot I can choose the color of each line, but when it shows the legend, the legend has different colors! How can I choose the colors inside the legend?

Risposte (1)

madhan ravi
madhan ravi il 29 Dic 2018
Modificato: madhan ravi il 29 Dic 2018
I am not certain what you want but try this example:
c={'r','g','m','k','b'}; % store the colors option in a cell array
for i =1:5
plot(rand(1,10),c{i}) % selects unique color for each iteration
hold on
end

3 Commenti

Maybe I had to put the entire code, sorry.
I want to plot on the same plot two types of curves, plus 1 reference line. To distinguish these curves during the iteration step, I make 1 color for step. For example the first two curves they've to be red, the seconds must be yellow, then green, blue... and the reference line another color, for example black, different from the previus colors. But when I run the code, the colors inside the Legend are random! So it shows tho red lines, two yellow lines... etc., How can I solve this problem?
clc
close all;
clear all;
global A1; % [1/s]
global Ea1_R; % [K]
global deltaHR1; % [cal/mol]
global A2; % [1/s]
global Ea2_R; % [K]
global deltaHR2; % [cal/mol]
%global Pin; % [Pa]
%global S; % [m2]
global CpMix; % [cal/kg/K]
global mtot; % [kg/h]
global Di; % [m]
global De; % [m]
global lambdaMix; % [cal/m/s/K]
global muMix; % [kg/m/s]
global Tfgas Tin; % [K]
global he; % [cal/m2/s/K]
global lambdaWall; % [cal/m/s/K]
%global vector_Twe; % [K]
% global vector_z; % [m]
global Ai Ae; % [m2]
%global a; % [1/m]
global Tfwalls
global beta Fse
global scokemax L lambdaCoke
% ------------------------------------------------------------------------%
% User data %
% ------------------------------------------------------------------------%
A1 = 2.2e12; % frequency factor [1/s]
Ea1_R = 29500; % activation temperature [K]
A2 = 7.3e11; % frequency factor [1/s]
Ea2_R = 28700; % activation temperature [K]
deltaHR1 = 18750; % reaction heat [cal/mol]
deltaHR2 = 30840; % reaction heat [cal/mol]
s = 0.01; % [m]
Di = 0.11; % internal diameter [m]
De = Di+2*s; % external diameter [m]
L = 70; % length [m]
a = 4/Di; % pw/S [1/m]
% C3H8 C2H4 CH4 C3H6 H2 H2O
MWi = [ 44.0962 28.0536 16.0426 ...
42.0804 2.0158 18.0152 ]/1000; % molecular weights of species [kg/mol]
%minlet = [2000 0 0 0 0 1000]/3600; % inlet mass flow rates [kg/s]
Tin = 600+273.15; % inlet temperature [K]
Pin = 3*101325; % pressure [Pa]
Tfgas = 1100+273.15; % temperature of external gases in the furnace [K]
he = 0.080*1000; % external heat exchange coefficient [cal/m2/s/K]
lambdaWall = 15/3600*1000; % thermal conductivity of tube wall [cal/m/s/K]
CpMix = 0.8*1000; % spcific heat [cal/kg/K]
muMix = 3.8e-5; % viscosity [kg/m/s]
lambdaMix = 3.194e-5*1000; % thermal conductivity [cal/m/s/K]
Tfwalls = 1100+273.15; % [K]
beta = 1e-8*1000/3600; % [cal/m2/s/K4]
Fse = 4e-8*1000/3600; % [cal/m2/s/K4]
scokemax = 0.01; % [m]
lambdaCoke = 3*1000/3600; % [cal/m/s/K]
TweFirstGuess = Tfgas;
Tmax = 1000; % [°C]
% ------------------------------------------------------------------------%
% 1. Calculations (pre-processing) %
% ------------------------------------------------------------------------%
%S = pi*Di^2/4; % cross section [m2]
Ai = pi*Di*L; % internal heat exchange area [m2]
Ae = pi*De*L; % external heat exchange area [m2]
N = 5;
col=hsv(N); %colors figure 2
for i=1:N
mtot_h = 2000+i*500-500; % [kg/h]
mtot = mtot_h/3600; % [kg/s]
minlet = [2/3*mtot 0 0 0 0 1/3*mtot]; % inlet mass flow rates [kg/s]
Fi0 = minlet./MWi; % [mol/s]
inlet = [Fi0 Tin TweFirstGuess Pin];
M = eye(9); M(8,8)=0;
options = odeset('Mass',M,'RelTol',1E-8,'AbsTol',1E-12);
[z,outlet] = ode15s(@PFR,[0 L],inlet,options);
FC3H8 = outlet(:,1); % [mol/s]
FC2H4 = outlet(:,2);
FCH4 = outlet(:,3);
FC3H6 = outlet(:,4);
FH2 = outlet(:,5);
FH2O = outlet(:,6);
T = outlet(:,7); % [K]
Twe = outlet(:,8);
P = outlet(:,9); % [Pa]
X = (Fi0(1)-FC3H8)/Fi0(1); % Propane conversion
fprintf('%.0f) Overall inlet flow rate: %.0f kg/h.\n',i,mtot_h)
fprintf('Propane conversion at the outlet section: %f.\n',X(end))
fprintf('The external temperature achieves %.2f °C.\n',max(Twe)-273.15)
fprintf('Inlet pressure: %.2f atm. Outlet pressure: %.2f atm. Pressure drop: %.2f atm.\n\n'...
,Pin/101325,P(end)/101325,Pin/101325-P(end)/101325)
figure
hold on
plot(z,T-273.15,z,Twe-273.15,'Color',col(i,:))
line = refline(0,Tmax); % horizzontal line at T=1100°C
line.Color = ('k');
xlabel 'Reactor length [m]', ylabel 'Temperature [°C]', title 'Temperature profiles'
%legend('Reactor T','External wall T','Tmax','Location','west')
legendInfo{i} = ['Mtot: ' num2str(mtot_h) ' kg/h'];
legendInfo{i+1} = 'T max';
legend(legendInfo,'Location','southeast') %,'Color',col(i,:))
end
% ------------------------------------------------------------------------%
% ODE system (defined locally, requires at least MATLAB-2016b) %
% ------------------------------------------------------------------------%
function dY_dz = PFR(z,Y)
global A1; % [1/s]
global Ea1_R; % [K]
global deltaHR1; % [cal/mol]
global A2; % [1/s]
global Ea2_R; % [K]
global deltaHR2; % [cal/mol]
%global Pin; % [Pa]
%global S; % [m2]
global CpMix; % [cal/kg/K]
global mtot; % [kg/s]
global Di; % [m]
global De; % [m]
global lambdaMix; % [cal/m/s/K]
global muMix; % [kg/m/s]
global Tfgas; % [K]
global he; % [cal/m2/s/K]
global lambdaWall; % [cal/m/s/K]
%global vector_Twe; % [K]
% global vector_z; % [m]
global Ai Ae; % [m2]
%global a; % [1/m]
global Tfwalls
global Fse beta
global scokemax L lambdaCoke
% Recovering variables
FC3H8 = Y(1); % [mol/s]
FC2H4 = Y(2);
FCH4 = Y(3);
FC3H6 = Y(4);
FH2 = Y(5);
FH2O = Y(6);
T = Y(7);
Twe = Y(8);
P = Y(9);
% Internal calculations
Ftot = sum(Y(1:6)); % [mol/s] NO! ALTRIMENTI RESTANO SEMPRE
% GLI STESSI!! INVECE CAMBIANO LUNGO IL REATTORE! PERCIO':
% Ftot = FC3H8+FC2H4+FCH4+FC3H6+FH2+FH2O; % [mol/s] NON è vero,
% funziona uguale
Ctot = P/T/8.314; % [mol/m3]
C_C3H8 = Ctot*FC3H8/Ftot;
k1 = A1*exp(-Ea1_R/T); % [1/s]
k2 = A2*exp(-Ea2_R/T);
r1 = k1*C_C3H8; % [mol/m3/s]
r2 = k2*C_C3H8;
RC3H8 = -r1-r2; % [mol/m3/s]
RC2H4 = r1;
RCH4 = r1;
RC3H6 = r2;
RH2 = r2;
RH2O = 0;
scoke = scokemax*z/L;
di = Di-2*scoke;
S = pi*di^2/4; % cross section [m2]
a = 4/di;
% Mass balance equations
dFC3H8_dz = S*RC3H8;
dFC2H4_dz = S*RC2H4;
dFCH4_dz = S*RCH4;
dFC3H6_dz = S*RC3H6;
dFH2_dz = S*RH2;
dFH2O_dz = S*RH2O;
% Energy balance equation
Q = Ftot/Ctot; % [m3/s]
rhoMix = mtot/Q; % [kg/m3]
v = Q/S; % [m/s]
Re = rhoMix*v*di/muMix;
nuMix = muMix/rhoMix;
alfaMix = lambdaMix/rhoMix/CpMix;
Pr = nuMix/alfaMix;
Nui = 0.023*Re^0.8*Pr^(1/3);
hi = Nui*lambdaMix/di;
Ue_1 = 1/(De/di/hi + De/2/lambdaWall*log(De/Di) + De/2/lambdaCoke*log(Di/di)); % [cal/m2/s/K]
Ui_1 = Ue_1*De/di;
F = he*(Twe-Tfgas)+beta*(Twe^4-Tfgas^4)+Fse*(Twe^4-Tfwalls^4)-Ue_1*(T-Twe);
qex = Ui_1*(T-Twe); % [cal/m2/s] *a[1/m]=[cal/m3/s]
Qr = -r1*deltaHR1-r2*deltaHR2; % [cal/m3/s]
dT_dz = S*(Qr-qex*a)/(mtot*CpMix);
% Pressure drop
f = 0.079/Re^(1/4);
tauWall = .5*rhoMix*v^2*f;
dP_dz = -tauWall*a;
% Collect derivatives
dY_dz = [dFC3H8_dz dFC2H4_dz dFCH4_dz dFC3H6_dz dFH2_dz dFH2O_dz dT_dz F dP_dz]';
end
Because you were using the same colours for the two datas , try the below:
plot(z,T-273.15,'Color',col(end-i+1,:))
hold on
plot(z,Twe-273.15,'Color',col(i,:))
Note: I don't recommend using globals parameterize your function instead.
But in this way it changes the colors of lines from the first plot

Accedi per commentare.

Prodotti

Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by