Flagging points with a different color when they are above a threshold line in a scatter plot

15 Ansichten (letzte 30 Tage)
I have generated scatter plots for data obtained from a csv file. In the plot, I would like to add a threshold line (as seen in the image attached) and flag the points appearing above the line a different color.
The code I have so far:
s1 = scatter(EHCU_Accel_VehicleLateral,EHCU_Accel_VehicleVertical);
axis([-1.2 1.2 -2 0.4]);
set(gcf, 'PaperPositionMode', 'auto');
s2 = scatter(EHCU_Accel_VehicleLongitudinal,EHCU_Accel_VehicleVertical);
axis([-1.2 1.2 -2 0.4]);
set(gcf, 'PaperPositionMode', 'auto');
Could anyone tell me how I could do this?

Antworten (1)

Vaidyanathan Thiagarajan
Vaidyanathan Thiagarajan am 3 Nov. 2017
Bearbeitet: Vaidyanathan Thiagarajan am 3 Nov. 2017
I do not fully understand your picture. However, I think this is what you need. The following code colors points to the left of a separator line in green and to its right in blue :
% Generate 1000 random points within a square of size 0.8 x 0.8
lowerLimit = -0.8;
upperLimit = -lowerLimit;
range = upperLimit - lowerLimit;
XY = range*rand(1000,1000)+lowerLimit;
X = XY(:,1);
Y = XY(:,2);
% Plot all points with a single color
s1 = scatter(X,Y,'b+');
axis([-1.1 1.1 -1.1 1.1]);
% Let the 45 degree line passing through origin be the separator
X_Line = -1:0.01:1;
Y_Line = X_Line;
hold on;
% Get the X and Y coordinates of the end point of the separator line
xv = [X_Line(1) X_Line(end)];
yv = [Y_Line(1) Y_Line(end)];
% Determine if the point is to the left or right of the separator line
% d will be negative if it is to the left otherwise it will be positive
d = (X - xv(1)).*(yv(2) - yv(1)) - (Y - yv(1)).*(xv(2) - xv(1) );
% Color points to the left in green
scatter(X(d < 0),Y(d < 0),'g+');
axis equal;
Hope it helps,

Community Treasure Hunt

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

Start Hunting!

Translated by