How to decide the color on the legend

1 次查看(过去 30 天)
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?

回答(1 个)

madhan ravi
madhan ravi 2018-12-29
编辑:madhan ravi 2018-12-29
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 个评论
madhan ravi
madhan ravi 2019-1-2
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.
Francesco Guaresi
But in this way it changes the colors of lines from the first plot

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by