Plot Multiple Colours automatically in a for loop

조회 수: 215 (최근 30일)
Robbie
Robbie 2012년 1월 11일
Hi,
I am trying to plot a graph, with a different colour being used automatically for each line on the plot, is there a simple way to do this? Here is my code:
% Define a range of y values
y = [0:0.01:1];
hold on
% Create a loop, which calculates the velocity profile for each a_0
% coefficient in the range 0 => 2.67 with 0.267 increments:
for a_0 = [0:0.267:2.67];
for i = 1:length(y)
% Define boundary layer thickness delta as 99% U_inf
delta = 0.99;
% Define eta as a function of y position and the constant delta:
eta(i) = y(i)./delta;
% Calculate the velocity profile:
u_prof(i) = (a_0.*eta(i)) - ((((3./2).*a_0)-2).*(eta(i).^2)) - ((1- (a_0./2)).*(eta(i).^4));
end
% Plot the velocity profile for each a_0 coefficient
plot (u_prof,y)
grid on
end
hold off
Thanks,
Robbie

채택된 답변

Chandra Kurniawan
Chandra Kurniawan 2012년 1월 11일
You should replace line
plot (u_prof,y)
with
plot (u_prof,y,'color',rand(1,3))
  댓글 수: 3
Rajan Kumar
Rajan Kumar 2014년 4월 25일
편집: Rajan Kumar 2014년 4월 25일
Why (1,3) can it be any other number?
Ilham Hardy
Ilham Hardy 2014년 4월 25일
No it can't.
To resembles the rgb input (1x3 matrix)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

C.J. Harris
C.J. Harris 2012년 1월 11일
I would suggest taking a look at the 'hold all' command. Just replace the line:
hold on
with:
hold all
That way you won't have to make any other modifications to your code.
  댓글 수: 3
Dili Sam
Dili Sam 2012년 11월 12일
THANKS IT WAS USEFUL FOR ME TOO... SAVED MY TIME!!
Taha Alian
Taha Alian 2015년 3월 27일
Excelent

댓글을 달려면 로그인하십시오.


Korosh Agha Mohammad Ghasemi
Korosh Agha Mohammad Ghasemi 2020년 12월 7일
%https://zil.ink/korosh -------- Ways to contact me ----------
% Korosh Agha Mohammad Ghasemi !
% Chemical Engineering at Shiraz University
x=linspace(0,2,100);
figure;
for a=[0.1 0.5 1 2 4]
y=x.^a; %The function is hypothetical
if a == 0.1 %Any color can be substituted
y=x.^a;
plot(x,y,'k') %Now choose the color
hold on
elseif a == 0.5
y=x.^a;
plot(x,y,'b') %Now choose the color
hold on
elseif a==1
y=x.^a;
plot(x,y,'g') %Now choose the color
hold on
elseif a==2
y=x.^a;
plot(x,y,'r') %Now choose the color
hold on
elseif a==4
y=x.^a;
plot(x,y,'y') %Now choose the color
hold on
grid on
end
end

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by