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

4 次查看(过去 30 天)
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);
xlabel('Lat');
ylabel('Vert');
axis([-1.2 1.2 -2 0.4]);
saveas(gcf,'ScatterLat1.jpg');
set(gcf, 'PaperPositionMode', 'auto');
s2 = scatter(EHCU_Accel_VehicleLongitudinal,EHCU_Accel_VehicleVertical);
xlabel('Long');
ylabel('Vert');
axis([-1.2 1.2 -2 0.4]);
saveas(gcf,'ScatterLong1.jpg');
set(gcf, 'PaperPositionMode', 'auto');
Could anyone tell me how I could do this?

回答(1 个)

Vaidyanathan Thiagarajan
编辑:Vaidyanathan Thiagarajan 2017-11-3
Hi,
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+');
xlabel('Lat');
ylabel('Vert');
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;
plot(X_Line,Y_Line,'k');
% 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,
Vaidyanathan

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by