# coloring points based on value

1 visualización (últimos 30 días)
mukesh bisht el 19 de Mayo de 2021
Comentada: KSSV el 19 de Mayo de 2021
I have x & y coordinates of points. Also each point has one of the two values (2,4). I want my point to be blue if value is 2 otherwise red. The way i am doing is taking too much time. Please suggest faster way
for z = 1:N_s
l = length(P{z});
a = 1;
while a <= l
x = P{z}(a,1); y = P{z}(a,2);
figure(1)
p_s = S{z}(a,1);
if p_s == 2
plot(x,y,'.', 'Color', colorstring(1)); hold on
else
plot(x,y,'.', 'Color', colorstring(2)); hold on
end
a = a + 1;
end
axis equal; hold on
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

KSSV el 19 de Mayo de 2021
You need not to use a loop. You can achieve this by getting the required indices and plotting. Check the beow demo example:
x = 1:10 ;
y = rand(size(x)) ;
% get values less than 0.5
idx = y<0.5 ;
% plot
figure
hold on
plot(x(idx),y(idx),'.b')
plot(x(~idx),y(~idx),'.r')
##### 2 comentariosMostrar NingunoOcultar Ninguno
mukesh bisht el 19 de Mayo de 2021
But in my case color is not based on its x or y value. Each point has a value (2 or 4) in addition to x & y coordinates. Color should be based on the value 2/4
KSSV el 19 de Mayo de 2021
That is not a problem......you can use index for the value and plot x,y. Let x,y,val be your values.
idx = val==2 ;
figure
hold on
plot(x(idx),y(idx),'.r')
plot(x(~idx),y(~idx),'.b')
Also have a look on the function gscatter. You can use this function as well.

Iniciar sesión para comentar.

### Categorías

Más información sobre Annotations en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by