automate scatter markers colours in groups
7 views (last 30 days)
Show older comments
Hello, I have a line plot with 28 points, and I want to colour the points in groups of 2, so points 1,2, 3,4, 5,6
I started with this
ax=app.UIAxes4;
[x,y] = getDataFromGraph(app,ax,1); % Get x,y, data from plot
hold(ax,'on');
n=numel(x); % Typically 28, but want to allow for any (even number)
Groups=2; % Want to allow for 2,3,4 or 5
% the number of point sis always a multiple of Groups
% I started to manually write c, but its too tediuos
c = [ 0 0 1; 0 0 1; 0 1 0; 0 1 0; 0 1 1; 0 1 1; 1 0 0; 1 0 0; 0 1 0; 1 0 0; 1 0 1; 0 1 1; 0 0 1; 0 1 0; 0 0 1; 0 1 0; 1 0 0; 1 0 1; 0 1 1; 0 0 1; 0 1 0; 0 0 1; 0 1 0; 1 0 0; 1 0 1; 0 1 1; 0 0 1; 0 1 0];
scatter(ax,x,y, [], c, 'filled')
Can 'c' be automated?
(You can see the first 6 points below)

Thanks
0 Comments
Accepted Answer
dpb
on 13 Mar 2025
Edited: dpb
on 13 Mar 2025
Alternate way to define the c vector index for lookup and using a colormap...
x=1:12; y=rand(size(x)); % make up some data...
N=numel(x); % the number of datapoints
G=2; % the groups
P=N/G; % passes -- is assumed N evenly divisible by G; should error check
ic=kron(1:N/G,ones(1,G)).'; % create index vector into COLORS array
colormap('hot')
scatter(x,y,[],ic,'filled')
indexes into the chosen colormap by the number of groups specified...
If you want specific colors, then create them as RGB triplets for your own colormap or use index into the array row with the indexing variable as
scatter(x,y,[],COLORS(ic,:),'filled')
where COLORS is your specific color RGB array you define.
6 Comments
dpb
on 13 Mar 2025
Edited: dpb
on 13 Mar 2025
To do it that way, you need to remap the index lookup to the size of the map...
myMap=[
0.47 0.25 0.80; % Color 1
0.83 0.14 0.14; % Color 2
0.25 0.80 0.54; % Color 3
1.00 0.54 0.00; % Color 4
0.3960 0.5090 0.9920; % Color 5
1 0 1; % Color 6
0.2270 0.7840 0.1920; % Color 7
1.0000 0.2700 0.2270]; % Color 8
N=26; G=2;
x=1:N; y=x.^2;
ic=kron(1:N/G,ones(1,G)).';
M=height(myMap); % will be 8 above
icc=mod(ic,M); icc(icc==0)=M; % remap to repeat range 1:M
Then, scatter using
scatter(x,y,[],myMap(icc,:),'filled')
will produce the sets of two (G) in sequence.
BTW, the MATLAB idiom to do the above (even though not needed here) would be
myMap=repmat(myMap,8,1); % expand to 8x original height
height(myMap)
More Answers (4)
dpb
on 13 Mar 2025
You need to either predefine n/Groups colors if you want them to be specific colors or you can use a linear index into a color map by group.
Then set the color as a lookup by the indexing expression based on Groups. An alternative and probably simpler coding solution would be to use hold on and multiple calls to scatter in a for...end loop; that way the color for the group is the same and just based on the index of the loop...
ax=app.UIAxes4;
[x,y] = getDataFromGraph(app,ax,1); % Get x,y, data from plot
hold(ax,'on');
n=numel(x); % Typically 28, but want to allow for any (even number)
Groups=2; % Want to allow for 2,3,4 or 5
COLORS='brgckmy'; % the standard letter mnemonics can handle up to seven groups - or build custom
% the number of point sis always a multiple of Groups
passes=n/Groups; % how many groups there are in the dataset to do...
hS=gobjects(passes,1); % preallocate for scatter handles so can modify later if desired...
i1=1; % the first index to first group
for ix=1:passes
i2=i1+Groups-1; % the second index in group based on first and size chosen
hS(ix)=scatter(ax,x(i1:i2),y(i1:i2), [], COLORS(ix), 'filled'); % and plot the points
i1=i2+1; % update the first index for next pass
end
Salt to suit...
0 Comments
Voss
on 13 Mar 2025
"Can 'c' be automated?"
Here is one way to automatically construct an n-by-3 matrix c with groups of repeated rows (colors), which you can use in scatter.
% somedata and axes to plot on
ax = gca();
x = 1:28;
y = 2*x.^2-5*x+3;
n = numel(x); % Typically 28, but want to allow for any (even number)
Groups = 3; % Want to allow for 2,3,4 or 5
number_of_groups = ceil(n/Groups);
% some set of colors to use
colors = get(ax,'ColorOrder');
n_colors = size(colors,1);
% replicate the colors so there are enough for each group of points
colors = repmat(colors,ceil(number_of_groups/n_colors),1);
% construct c by appropriate indexing into the rows of colors
idx = repelem(1:number_of_groups,1,Groups);
idx = idx(1:n);
c = colors(idx,:)
% make the scatter plot
scatter(ax,x,y, [], c, 'filled')
Image Analyst
on 13 Mar 2025
There is a function for exactly that, which makes it easy to color markers by group. It's called gscatter
help gscatter
2 Comments
dpb
on 13 Mar 2025
gscatter(x,y,g,clr,sym,siz,doleg,xnam,ynam)
I had forgotten there is gscatter, indeed. It has to have the grouping variable as the third argument; that could be the ic indexing vector.
Jason
on 13 Mar 2025
Edited: Jason
on 13 Mar 2025
2 Comments
Voss
on 13 Mar 2025
Here's a way to programmatically determine the lower of each pair and remove those rows. (In case of an odd initial number of rows, so that one row is unpaired, the unpaired row is preserved.)
data = readmatrix('PairedData.txt');
N = size(data,1);
if mod(N,2)
data(end+1,:) = NaN;
N = N+1;
end
[~,idx] = min(reshape(data(:,2),2,[]),[],1);
rows_to_keep = 2*(0:N/2-1)+idx;
data_keep = data(rows_to_keep,:);
plot(data(:,1),data(:,2),'s-')
hold on
plot(data_keep(:,1),data_keep(:,2),'x-')
See Also
Categories
Find more on Graphics Object Programming 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!