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

4 comentarios

John Doe
John Doe el 12 de Jul. de 2019
Can you show an example plot?
If you want to do a scatter plot this is easy:
scatter(x,y,100,z)
Where 100 just refers to the size of the point.
Michel tawil
Michel tawil el 12 de Jul. de 2019
thank you for your reply,
Scatter will not work because it only shows a point while i need to plot a line then color it
Part of the code :
p1(i,:)=[reference(1,i) y];% just making a point 1 in order to plot the line
p2(i,:)=[reference(1,i)+x(1,i) y]; % this is the second point of my line
averageV=averageV/max(averageV); % this is the value that i need to color the line with
for j=1:240
plot([ p1(j,1) p2(j,1)], [p1(j,2) p2(j,2)]);
end
each one of the 240 lines plotted corresponds to a value of averageV, i want the line to take a color according to that value
John Doe
John Doe el 12 de Jul. de 2019
You can modify the following code for your purposes.
where n will = the size of averageV
y(1:241,1) = 1:241
n = size(y, 1);
colors = hsv(n);
h = line([0 10], [y(1:241,1) y(1:241,1)])
set(h, {'color'}, num2cell(colors, 2));
Michel tawil
Michel tawil el 13 de Jul. de 2019
Hey, if it's only taking the size, how will it plot according to the value of averageV,
i want the line color plotted in XY according to the value of averageV not according to the size of averageV.
sorry for bothering again

Iniciar sesión para comentar.

 Respuesta aceptada

Cristian Garcia Milan
Cristian Garcia Milan el 12 de Jul. de 2019

0 votos

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!

4 comentarios

Michel tawil
Michel tawil el 13 de Jul. de 2019
hello critian,
thanks
this command does not work because each line of the 240 lines, is plotted accorrding to two points for j=1:240
plot([ p1(j,1) p2(j,1)], [p1(j,2) p2(j,2)]);
end
but adding ,'color',[Z(i) 0 1]) to it makes it work. still need to add the colorbar according to the color we specified because it ranges from 0.00001 to 1. adding colorbar in the command does not help since the value are of huge number. do you know any trick ?
thanks
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
Michel tawil el 17 de Jul. de 2019
thank you, i guess this is exactly what i want. i'll make sure of it
Michel tawil
Michel tawil el 14 de Sept. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 12 de Jul. de 2019

Comentada:

el 14 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by