Plot points in a different colour depending on co ordinates
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (4)
bym
am 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
0 Kommentare
Image Analyst
am 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')
0 Kommentare
Walter Roberson
am 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Scatter Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!