How to separate points under a line in a scatter plot
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
UH
am 1 Nov. 2022
Bearbeitet: Eric Delgado
am 1 Nov. 2022
I have x-y data set. I want to plot these data and then pick all the coordinates under a specific line.
My code is given below:
clc
clear all
close all
%Importing xy data
Datax= load('x.mat');
x = Datax.x;
Datay= load('y.mat');
y = Datay.y;
%plotting the data
scatter(x,y,".", 'LineWidth',2,MarkerEdgeColor="#1A5276", SizeData=40)
hold on
line ([0 2000], [0 200],'LineStyle','--','color',[0.5, 0.5, 0.5],'linewidth',1)
xlabel('x', 'Color','k');
ylabel('y', 'Color','k');
In the output above, I want to pick the x-y coordinates of the points that lie under the line. For now I am working in a very counterintuitive way by first counting the number of points (4 points in this graph), then I use
g = ginput(4);
After this I click on all those points. Then I use
D = pdist2([x y],g);
[~,ix] = min(D);
to find the indices of those points.
However, I have data set of more than 50,000 points and it is very exhausting. Is there a way to automate this procedure?
Much appreciated. Thanks
0 Kommentare
Akzeptierte Antwort
Eric Delgado
am 1 Nov. 2022
Bearbeitet: Eric Delgado
am 1 Nov. 2022
Try this...
% Data
xData = Datax.x;
yData = Datay.y;
% Reference line: y = ax + b
% Using your data, a = 200/2000 and b = 0
yLine = .1 * xData;
idx = yLine > yData;
% Plot
figure
scatter(xData(~idx), yData(~idx), ".", 'LineWidth', 2, MarkerEdgeColor="#1A5276", SizeData=40)
hold on
scatter(xData(idx), yData(idx), "ro")
line([0, max(xData)], [0, .1*max(xData)], 'LineStyle', '--', 'color', [0.5, 0.5, 0.5], 'linewidth', 1)
xlabel('x', 'Color','k');
ylabel('y', 'Color','k');
hold off
% Index of the data under reference line
idx = find(idx)
0 Kommentare
Weitere Antworten (1)
KSSV
am 1 Nov. 2022
Refer the theory here: https://www.emathzone.com/tutorials/geometry/position-of-point-with-respect-to-line.html
%Importing xy data
Datax= load('x.mat');
x = Datax.x;
Datay= load('y.mat');
y = Datay.y;
%plotting the data
scatter(x,y,".", 'LineWidth',2,MarkerEdgeColor="#1A5276", SizeData=40)
hold on
line ([0 2000], [0 200],'LineStyle','--','color',[0.5, 0.5, 0.5],'linewidth',1)
xlabel('x', 'Color','k');
ylabel('y', 'Color','k');
% get the line m and c
p = polyfit([0; 2000],[0; 200],1) ;
m = p(1) ; c = p(1) ;
dy = y-(m*x+c) ;
idx1 = dy > 0 ; % lie above the line
idx2 = dy < 0 ; % lie below the line
plot(x(idx1),y(idx1),'or')
plot(x(idx2),y(idx2),'og')
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Exploration 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!