# Plots with different colors

2 views (last 30 days)
Yen Tien Yap on 25 Aug 2021
Edited: Simon Chan on 25 Aug 2021
Hi, I want to create the scatter plot with three different colours represents three different regions. I think something wrong with the for loop, can someone help me with that? Thank you.
rho=1000; %[kg/m3]
D=12.6*10^-3; %[m]
L=1.5; %[m]
miu=0.001; %[Pas]
g=9.81;
A=pi*D^2/4;
Q0=[1600,1500,1400,1300,1200,1100,1000,900,800,700,600,500,400,300,240,220,...
200,180,160,140,120,100,80,70,70,60,50,40,30,20,10];%[L/hr]
Q=Q0/(1000*3600);
%Wet-wet digital gauge
P_dpg=[20.1,17.5,15.7,13.1,11.6,9.3,8,6.5,5.3,4.1,3,2.1,1.3,0.8]; %[kPa]
%Inverted manometer
h=[6.9,5.9,5,4.1,3.2,2.6,1.8,1.3,0.7,0.5]; %[cm]
h_m=h/100;
P_mtr=rho*g*h_m;
%Capsuhelic gauge
P_cpg=[33,31,28,23,17,12,8];
P=[P_dpg.*1000,P_mtr,P_cpg];
V=Q/A;
Re=rho*V*D/miu;
f=P*D./(2*rho*L*V.^2);
X=Re(25:31);
Y=f(25:31);
p=polyfit(log(X),log(Y),1)
p = 1×2
-1.2271 4.1752
y=polyval(p,log(X));
figure(1)
for i=1:length(Re)
if Re(i) < 2*10^3
loglog(Re,f,'xb','LineWidth',1)
elseif Re(i)>=2*10^3 & Re(i)<=3*10^3
loglog(Re,f,'xr','LineWidth',1)
else
loglog(Re,f,'xg','LineWidth',1)
end
end
hold on
loglog(X,exp(y),'--r','LineWidth',1.5)
grid on
xlim([10^2 10^5])
ylim([0.001 0.1])
xlabel('Reynolds number Re')
ylabel('Friction factor f')
title('f vs Re')

Simon Chan on 25 Aug 2021
Put the index and hold on inside the loop
figure(1)
for i=1:length(Re)
if Re(i) < 2*10^3
loglog(Re(i),f(i),'xb','LineWidth',1)
hold on
elseif Re(i)>=2*10^3 & Re(i)<=3*10^3
loglog(Re(i),f(i),'xr','LineWidth',1)
hold on
else
loglog(Re(i),f(i),'xg','LineWidth',1)
hold on
end
end
Simon Chan on 25 Aug 2021
It is better to separate the data into 3 groups since the plot was performed point by points and hence there were actually 31 data and hence the legend above is showing the last 3 points only.
The code needs to be adjusted like the following:
figure(1)
Re_A = Re(Re < 2*10^3); % Group 1
f_A = f(Re < 2*10^3);
Re_B = Re(Re>=2*10^3 & Re<=3*10^3); % Group 2
f_B = f(Re>=2*10^3 & Re<=3*10^3);
Re_C = Re(Re > 3*10^3); % Group 3
f_C = f(Re > 3*10^3);
loglog(Re_A,f_A,'xb','LineWidth',1) % Plot Group 1 data
hold on % In this case, one hold on is enough
loglog(Re_B,f_B,'xr','LineWidth',1) % Plot Group 2 data
loglog(Re_C,f_C,'xg','LineWidth',1) % Plot Group 3 data
legend('Laminal flow', 'Transitional zone', 'Turbulent flow')