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.

 Accepted Answer

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.

More Answers (1)

"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

Products

Release

R2026a

Asked:

on 10 Sep 2026 at 10:47

Commented:

on 17 Sep 2026 at 19:38

Community Treasure Hunt

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

Start Hunting!