Different coloured regions on a scatter plot
Show older comments
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.
Answers (2)
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 Comments
Star Strider
about 12 hours ago
Edited: Star Strider
about 1 hour ago
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
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

