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 个评论

Felicity
Felicity 2026-9-10,11:42
编辑:Felicity 2026-9-10,11:50
Sorry I forget not everyone is a motorsport nerd. To put it in context, I'm doing an analysis to see lateral vs longitudinal acceleration recorded by a race car over the course of 1 lap of a circuit.
This is an image of a different one I found online that I think illustrates what I want a bit better (although I don't need the lines around or between the colours, just colouring the points would be enough). 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. That way I can see what % of the lap time was spent doing each activity.
Thank you very much for the response, I'll have a play around with your suggestions.
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
See if the inpolygon function does what you want. (I'm still not absolutely certain what that is.)
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)
nron = 0
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
scatter - Scatter plot Syntax Vector and Matrix Data scatter(x,y) scatter(x,y,sz) scatter(x,y,sz,c) scatter(___,"filled") scatter(___,mkr) Table Data scatter(tbl,xvar,yvar) scatter(tbl,xvar,yvar,"filled") Additional Options scatter(ax,___) scatter(___,Name,Value) s = scatter(___) Input Arguments x - x-coordinates scalar | vector | matrix y - y-coordinates scalar | vector | matrix sz - Marker size 36 (default) | numeric scalar | row or column vector | matrix | [] c - Marker color color name | RGB triplet | matrix of RGB triplets | vector of colormap indices mkr - Marker symbol "o" (default) | "+" | "*" | "." | "x" | ... "filled" - Option to fill interior of markers "filled" tbl - Source table table | timetable xvar - Table variables containing x-coordinates one or more table variable indices yvar - Table variables containing y-coordinates one or more table variable indices ax - Target axes Axes object | PolarAxes object | GeographicAxes object Name-Value Arguments MarkerEdgeColor - Marker outline color "flat" (default) | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... MarkerFaceColor - Marker fill color "none" (default) | "flat" | "auto" | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... LineWidth - Width of marker edge 0.5 (default) | positive value ColorVariable - Table variable containing color data table variable index Output Arguments s - Scatter object Scatter object | array of Scatter objects Examples openExample('graphics/CreateScatterPlotExample') openExample('graphics/VaryCircleSizeExample') openExample('graphics/VaryCircleColorExample') openExample('graphics/ScatterColorPaletteExample') openExample('graphics/VaryCircleSizeandColorExample') openExample('graphics/SpecifyMarkerSymbolExample') openExample('graphics/SpecifyMarkerPropertiesExample') openExample('graphics/ScatterAlphaDataExample') openExample('graphics/ScatterTableExample') openExample('graphics/ScatterTableSizeColorExample') openExample('graphics/ScatterSpecifyAxes19bExample') openExample('graphics2/SetScatterObjectPropertiesExample') See also hold, plot, scatter3, bubblechart, swarmchart, Scatter Properties Introduced in MATLAB before R2006a Documentation for scatter doc scatter Other uses of scatter tall/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 的更多信息

产品

版本

R2026a

提问:

2026-9-10,10:47

回答:

2026-9-13,14:46

Community Treasure Hunt

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

Start Hunting!

Translated by