MATLAB: How to give the lines color based on the distanse

plotting

My plot below shows the oscillatory displacement of particles vs time in a pipe. Each line coreponds to each particle. Particles are released in different position from the center of pipe along the radius. How can I show each line in this plot in a different color?(My goal is the line's color changes with the distance from the center of pipe(r)).
r=sqrt(Xp^2+Yp^2): position of each particle in rdaial direction.Xp and Yp are the particle position in cartesian.

Best Answer

Try this (untested)
cmap = jet(length(Xp));
r = sqrt(Xp .^ 2 + Yp .^ 2);
r = round(rescale(r, 1, length(Xp)));
for k = 1 : length(Xp)
thisColor = cmap(r(k), :);
plot(Xp(k), Yp(k), '.', 'Color', thisColor, 'MarkerSize', 4)
end
It will plot dots so make sure you have enough points to make the dots be adjacent and look like a line. You can adjust the MarkerSize if you need to.