Plot points in a different colour depending on co ordinates

4 views (last 30 days)
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;
radius = 30;
theta = rand(1,num1)*(2*pi);
r = sqrt(rand(1,num1))*radius;
xCor = r.*cos(theta);
yCor = r.*sin(theta);
plot(xCor,yCor,'rx','LineWidth',2,'MarkerSize',10)
subplot(1,1,1)
hold on;
num = 1000;
rad = 10;
theta = rand(1,num)*(2*pi);
r = sqrt(rand(1,num))*rad;
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

Answers (4)

bym
bym 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

Image Analyst
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')

Walter Roberson
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.

Andy
Andy on 19 Feb 2013
Thank you for the replies. I managed to work it out with a combination in the end. In the end i used a for loop, going through my generated numbers. I then checked the distance of the co ordinates away from the centre point of my circle and plotted them with the right colour accordingly.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!