How to separate points under a line in a scatter plot

12 ビュー (過去 30 日間)
UH
UH 2022 年 11 月 1 日
編集済み: Eric Delgado 2022 年 11 月 1 日
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

採用された回答

Eric Delgado
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)

その他の回答 (1 件)

KSSV
KSSV 2022 年 11 月 1 日
%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')

カテゴリ

Help Center および File ExchangeData Exploration についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by