How to separate points under a line in a scatter plot

조회 수: 24 (최근 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 CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by