How can I plot multiple lines in different colors on a single plot using loops?

873 Ansichten (letzte 30 Tage)
I am evaluating and plotting a function of time using at multiple times using a for loop and I want each line to plot a different color. My code plots all the lines the same color. At first my legend was not matching the lines so I am trying to plot the lines with defined colors and then change my legend accordingly. If anyone knows why the legends colors are out of order with the plot that would also help!
Cs is the function that varies with time.
Can someone suggest another way of doing this?
line_color = ['b' 'g' 'y' 'c' 'm' 'r'];
for i=1:length(t);
figure(1)
title('Carbon Sequestration in Indoor Environment vs. SA/V Ratio','FontSize',10)
ylabel('Carbon Sequestration (kg CO2)')
xlabel('SA/V Ratio')
legend('25 years','50 years','75 years','100 years','125 years','150 years','AutoUpdate','off')
hold on
plot(SAV_ratio,Cs,line_color(i),'LineWidth',2)
end
Thanks
  3 Kommentare
JoelB
JoelB am 15 Sep. 2018
I know something isnt right but I am not sure how to approach this using a for loop. Maybe there is a better way to ensure the plot matches the legend. Here is the full code:
L=3; % measured in m
vtotal=0.75; % measured in m3
phi = 1.0; % assumed to be 1.0
alpha = 0.165;
beta = 0;
y = 0;
k0=3;
k1=1;
k2=1;
ppm=800;
n=0;
c=ppm/516e+3;
strength=40; % measured in MPa
m=(strength+7.4)/0.0825;
R=0.0016*strength^3.106;
line_color = ['b' 'g' 'y' 'c' 'm' 'r'];
t = [25 50 75 100 125 150]; % measured in years
for i = 1:length(t)
x(i)=(2*c*t(i)/R)^0.5*(k0*k1*k2)^0.5*(1/t(i))^n;
end
SAV_ratio = [0:1:30]; % SA to Volume ratio
SA=SAV_ratio.*vtotal;
% this section calculates the sequestered carbon for each time, t, and for various
% surface areas. The if statement ensures the Vc does not exceed the volume of concrete.
for i = 1:length(t)
for h = 1:length(SA)
if x(i)*SA(h) >= vtotal
Cs(i,h) = phi*(alpha-beta*y)*vtotal*m;
else
Cs(i,h) = phi*(alpha-beta*y)*x(i)*SA(h)*m;
end
end
end
for i=1:length(t);
figure(1)
title('Carbon Sequestration in Indoor Environment vs. SA/V Ratio','FontSize',10)
ylabel('Carbon Sequestration (kg CO2)')
xlabel('SA/V Ratio')
legend('25 years','50 years','75 years','100 years','125 years','150 years','AutoUpdate','off')
hold on
plot(SAV_ratio,Cs,line_color(i),'LineWidth',2)
end
Image Analyst
Image Analyst am 15 Sep. 2018
Joel, did you even see my answer below in the official "Answer" section of this page?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Abby Skofield
Abby Skofield am 20 Sep. 2023
Starting in R2019b, use the colororder command to specify a palette of colors for individual plots to use. You can pass in color names like "b" and "blue" directly to the colororder command if those are the desired colors.
x = linspace(0,4*pi,100);
y = sin(x)+(1:6)';
plot(x,y,'LineWidth',2)
line_color = ["b" "g" "y" "c" "m" "r"];
colororder(line_color)
Starting in R2023b, there are several predefined, named palettes that can be used with the colororder command, for example'reef', 'meadow' and 'dye'.
clf
t = tiledlayout('horizontal');
ax1 = nexttile;
plot(ax1,x,y,LineWidth = 2)
colororder(ax1,'reef')
title('reef')
ax2 = nexttile;
plot(ax2,x,y,LineWidth = 2)
colororder(ax2,'meadow')
title('meadow')
ax3 = nexttile;
plot(ax3,x,y,LineWidth = 2)
colororder(ax3,'dye')
title('dye')
  4 Kommentare
Adam Danz
Adam Danz am 7 Jan. 2024
@Qian, you could also use
line_color = ["#66c2a5","#fc8d62","#8da0cb","#e78ac3","#a6d854"];
Qian
Qian am 7 Jan. 2024
Thank you! It works with the doule quotation marks and [].

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Image Analyst
Image Analyst am 15 Sep. 2018
Try this. Adapt as needed for your signal.
line_color = ['b' 'g' 'y' 'c' 'm' 'r'];
t = 1 : 100;
SAV_ratio = t;
ca = cell(1, length(line_color));
period = 100;
for k = 1 : length(line_color)
ca{k} = sprintf('%d years', k*25);
hold on
% Get new values.
Cs = sin(2*pi*t / (period * k));
plot(SAV_ratio, Cs,'-', 'Color', line_color(k),'LineWidth',2)
grid on;
end
title('Carbon Sequestration in Indoor Environment vs. SA/V Ratio','FontSize',16)
ylabel('Carbon Sequestration (kg CO2)')
xlabel('SA/V Ratio')
ax = gca;
ax.XAxisLocation = 'origin';
legend(ca, 'Location', 'southwest')

Ivan Popov
Ivan Popov am 16 Jun. 2021
Bearbeitet: Ivan Popov am 16 Jun. 2021
You have to use colororder(Colormap Name) with the desired color map. Then with one plot, you can use all desired colors in the appropriate order. Actually one can change colororder after data is plotted.
Example:
colororder(hsv(x));
plot(AnyMatrix(:,1:x));

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by