MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Plot points in a different colour depending on co ordinates

Asked by Andy on 18 Feb 2013

I want to be able to plot my points in a scatter plot and if the points are within the co ordinates of two circles they will be plotted in different colours. At the moment i can plot my points within the limits i need.

```      x = randi([-35,165],[2000,1]);
x = x';
y = randi([-20,80],[2000,1]);
y = y';
plot(x,y,'kx','LineWidth',2,'MarkerSize',10)```

I can also plot points with different colours within two circles.

```      num1 = 2000;
```      theta = rand(1,num1)*(2*pi);
xCor = r.*cos(theta);
yCor = r.*sin(theta);```
`      plot(xCor,yCor,'rx','LineWidth',2,'MarkerSize',10)`
```      subplot(1,1,1)
hold on;
num = 1000;
theta = rand(1,num)*(2*pi);
xCor2 = r.*cos(theta);
yCor2 = r.*sin(theta);
plot(xCor2,yCor2,'gx','LineWidth',2,'MarkerSize',10)```

What i cant seem to work out is how do i use the original points in my first section of code and then change the colour points which are within the limits of a circle (with a given centre point)? I am aiming not to use loops as well.

Thanks You

## Products

No products are associated with this question.

Answer by proecsm on 19 Feb 2013

Maybe this will help as an example:

```clc;clear
x = rand(1000,1);
y = rand(1000,1);
yg = y;
yg(hypot(x-.5,y-.5)<.25)=NaN;
yb = y;
yb(hypot(x-.5,y-.5)>=.25)=NaN;
plot(x,[yg yb],'.')
axis square
```

Answer by Image Analyst on 19 Feb 2013

And a slight variation where the points continuously vary color from the center of the circle:

```x = rand(1000,1);
y = rand(1000,1);
distances = ((x-.5).^2 + (y-0.5).^2).^0.5;
% Normalize - divide my sqrt(maxX^2 + maxY^2)
distances = distances / sqrt(.5^2 + .5^2);
[sortedDistances sortIndexes] = sort(distances);
% Arrange the data so that points close to the center
% use the blue end of the colormap, and points
% close to the edge use the red end of the colormap.
xs = x(sortIndexes);
ys = y(sortIndexes);
cmap = jet(length(x)); % Make 1000 colors.
scatter(xs, ys, 10, cmap, 'filled')
grid on;
title('Points where color depends on distance from center', ...
'FontSize', 30);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
```

Answer by Walter Roberson on 19 Feb 2013

Use scatter() instead of plot(). The C (color) parameter of scatter() can be an array of RGB rows, one row per point.