How to separate points under a line in a scatter plot
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Eric Delgado
2022-11-1
编辑:Eric Delgado
2022-11-1
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 个评论
更多回答(1 个)
KSSV
2022-11-1
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!