Different coloured regions on a scatter plot
显示 更早的评论
I have a g-g diagram where I want to display different regions in different colours (so right acceleration is one colour, pure acceleration is a different one, left acceleration is different, pure left cornering is different and so on). I was able to overlay a plot on top to show left, right and in the middle like this:
sz = 8
scatter(Lat_A, Long_A, sz, 'filled')
ylabel("Longitudinal Acceleration /g")
xlabel("Lateral Acceleration /g")
threshold = 0.4
right = Lat_A(Lat_A>threshold)
left = Lat_A(Lat_A<-threshold)
hold on;
scatter(right, Long_A(Lat_A > threshold), sz, 'g', 'filled');
scatter(left, Long_A(Lat_A < -threshold), sz, 'r', 'filled');
hold off;
However, I'm struggling to factor in the braking and acceleration the same way on the same plot. In simple terms, I want to show (x>0.4, y>0.4), (x<-0.4, y>0.4), (abs(x)< 0.4, y>0.4) etc. as different colours. I'm pretty new to MATLAB so any help would be appreciated.
回答(3 个)
Depicting regions such as: '(x>0.4, y>0.4), (x<-0.4, y>0.4), (abs(x)< 0.4, y>0.4) etc.' would require a patch plot. These can be overlaid with your current plot, however it would be best to change the 'FaceAlpha' property to something less than 1 in order to show the other data clearly. (I had to look up 'g-g diagram.)
I am not certain what you want to do, however something like this could work --
figure
patch([-4 0 0 4], [4 0 0 4], 'g', FaceAlpha=0.5, EdgeColor='none')
axis([-5 5 -1 5])
grid
The patch arguments have to describe a closed region, so at least 4 values for each one.
.
2 个评论
Airplanes for me. I'm an Instrument-Rated Private Pilot, so cars only to get to and from the airport!
'The only thing is I want to have the points in each region stored somehow so I can then see how many fall within each region
For the region colours, I still recommend patch.
EDIT -- (11 Sep 2026 at 00:23)
Using inpolygon to determine the points inside and outside a region, then plotting them --
t = linspace(0, 2*pi, 500).';
r = 1;
c = r.*[cos(t) sin(t)];
datax = randn(250,1);
datay = randn(250,1);
[in,on] = inpolygon(datax, datay, c(:,1), c(:,2));
nron = nnz(on)
figure
plot(c(:,1), c(:,2), DisplayName='Region of Interest')
hold on
scatter(datax(in), datay(in), '.g', DisplayName='In Region')
scatter(datax(~in), datay(~in), '.r', DisplayName='Outside of Region')
if nron ~= 0
scatter(datax(on), datay(on), '.m', DisplayName='On Border')
end
hold off
grid
axis('equal')
xlabel('X')
ylabel('Y')
legend(Location='best')
pctpoints = nnz(in)/size(datax,1) * 100;
title(sprintf('Region Contains %.2f%% Of The Data',pctpoints))
.
"just colouring the points would be enough" <== One of the inputs to scatter is a vector of colors that you want each marker point to have.
help scatter
Try this:
fontSize = 25;
numPoints = 3000;
lateralAcceleration = 7 * rand(numPoints, 1) - 3.5;
longitudinalAcceleration = 4.2 * rand(numPoints, 1) - 3;
% Assign colors (determined via Photoshop from screenshot of sample image).
red = [254, 1, 2] / 255;
blue = [0, 81, 148] / 255;
green = [0, 177, 80] / 255;
purple = [113, 50, 162] / 255;
yellow = [254, 192, 0] / 255;
brown = [116, 63, 32] / 255;
orange = [229, 111, 35] / 255;
cyan = [39, 161, 254] / 255;
% Initialize size and have them be all black initially.
markerColors = zeros(numPoints, 3);
% Assign red markers
indexes = longitudinalAcceleration > 0.25 & lateralAcceleration < -0.3;
markerColors(indexes, :) = repmat(red, sum(indexes), 1);
% Assign purple markers
indexes = longitudinalAcceleration > 0.25 & lateralAcceleration > -0.3 & lateralAcceleration < 0.3;
markerColors(indexes, :) = repmat(purple, sum(indexes), 1);
% Assign green markers
indexes = longitudinalAcceleration > 0.25 & lateralAcceleration > 0.3;
markerColors(indexes, :) = repmat(green, sum(indexes), 1);
% Assign blue markers
indexes = longitudinalAcceleration < -0.3 & lateralAcceleration < -0.3;
markerColors(indexes, :) = repmat(blue, sum(indexes), 1);
% Assign brown markers
indexes = longitudinalAcceleration < -0.3 & lateralAcceleration > -0.3 & lateralAcceleration < 0.3;
markerColors(indexes, :) = repmat(brown, sum(indexes), 1);
% Assign yellow markers
indexes = longitudinalAcceleration < -0.3 & lateralAcceleration > 0.3;
markerColors(indexes, :) = repmat(yellow, sum(indexes), 1);
% Assign orange markers
indexes = abs(longitudinalAcceleration) <= 0.3 & lateralAcceleration < -0.3;
markerColors(indexes, :) = repmat(orange, sum(indexes), 1);
% Assign cyan markers
indexes = abs(longitudinalAcceleration) <= 0.3 & lateralAcceleration > 0.3;
markerColors(indexes, :) = repmat(cyan, sum(indexes), 1);
% Do the scatterplot
scatter(lateralAcceleration, longitudinalAcceleration, 35, markerColors, 'filled');
grid on;
xlabel('Lateral Acceleration', 'FontSize', fontSize);
ylabel('Longitudinal Acceleration', 'FontSize', fontSize);
xline(0.3, 'LineWidth', 3);
xline(-0.3, 'LineWidth', 3);
yline(0.3, 'LineWidth', 3);
yline(-0.3, 'LineWidth', 3);
You can use text() to add the colored text labels.
类别
在 帮助中心 和 File Exchange 中查找有关 Scatter Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



