automate scatter markers colours in groups
이전 댓글 표시
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
채택된 답변
추가 답변 (4개)
dpb
2025년 3월 13일
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...
"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')
There is a function for exactly that, which makes it easy to color markers by group. It's called gscatter
help gscatter
댓글 수: 2
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-')
Jason
2025년 3월 14일
카테고리
도움말 센터 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





