Asked by Michel tawil
on 12 Jul 2019

hey, i have many lines in X and Y axes, need to color each line according to a variable Z, how to do that ?

x

y

already plotted all the lines needed but need to add the color for each line according to the value of Z. for example if Z= 0 the line will take a color and if it;s 0.8 it takes another color( i mean how to add a colorbar for this )

thanks

Answer by Cristian Garcia Milan
on 12 Jul 2019

Accepted Answer

Hello Michel,

I think you can use RGB colors so if you have the data in rows in X and Y it would be somethink like:

figure hold on for i = 1:size(X,1) plot(X(i,:),Y(i,:),'color',[Z(i) 0 1]) % There is needed that Z has a value from 0 to 1 for each row end

Hope it helps!

Cristian Garcia Milan
on 14 Jul 2019

Okey, so you can use plot3()

for j=1:240

plot3([ p1(j,1) p2(j,1)], [p1(j,2) p2(j,2)],[Z(i),Z(i)]);

end

colorbar

Z would be any high, and MATLAB will refer that with a color going from blue to yellow.

Is that what you want?

Michel tawil
on 17 Jul 2019

thank you, i guess this is exactly what i want. i'll make sure of it

Michel tawil
on 14 Sep 2019

hello again, the plot3 didn't work.

My point is that for each line i am plotting, i want it to be colored according to a third variable ( that is the velocity of that line), the index of the lines and variables is not a problem, i just need a function in matlab that takes the color from a matrix different than the one used to plot(3rd variable)

nothing worked till now. i have 235 lines and each one of them has a velocity

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## John Doe (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/471514-2d-plot-with-3rd-variable#comment_724037

## Michel tawil (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/471514-2d-plot-with-3rd-variable#comment_724038

## John Doe (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/471514-2d-plot-with-3rd-variable#comment_724041

## Michel tawil (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/471514-2d-plot-with-3rd-variable#comment_724132

Sign in to comment.