Change properties in gscatter, based on the group label

5 views (last 30 days)
How to set some specific properties for a specific group (i.e. marker, markersize, one specific color), let's say for group labelled as "-1", and set the same properties (i.e. marker, markersize, except for colors, that should be different) for the remaining groups, i.e. groups labelled as "1,2,3,4"?
a = [37 98 1; 35 94 1; 33 97 1; 33 99 1; 31 96 1; 30 97 1; 29 100 1; 30 94 1; 31 90 1; 28 93 1; 21 63 1; 20 61 1; 22 60 1; 24 60 1; 24 64 1; 27 62 1; 28 65 1; 27 59 1; 23 57 1; 26 56 1; 27 57 1; 29 57 1; 31 60 1; 31 61 1; 31 64 1; 32 66 1; 36 60 1; 37 56 -1; 32 56 1; 31 54 1; 27 53 1; 13 43 2; 17 42 2; 17 40 2; 19 37 2; 14 40 2; 13 42 2; 10 41 2; 10 42 2; 9 39 2; 6 40 2; 25 20 3; 24 18 3; 26 18 3; 27 19 3; 27 21 3; 29 21 3; 30 22 3; 30 23 3; 31 26 3; 33 27 3; 35 28 3; 94 11 4; 95 10 4; 97 8 4; 94 8 4; 93 8 4; 99 12 4; 97 12 4; 98 15 4; 95 14 4; 91 15 4; 92 17 4; 91 19 4; 89 19 4; 87 21 4; 89 22 4; 86 23 4; 84 25 4; 81 25 4; 80 26 4; 82 26 4; 79 29 4; 82 29 4; 83 29 4; 86 28 4; 88 25 4; 91 24 4; 93 21 4; 94 22 4; 94 19 4; 95 18 4; 97 20 4];
g = gscatter(a(:,1),a(:,2),a(:,3));
I was thinking about something like this, but I do not know how to efficiently change properties in g = gscatter:
if unique(a(:,3)) == -1
'marker'=
'markersize'=
...
elseif unique(a(:,3)) ~= -1
'marker'=
'markersize'=
...
end

Accepted Answer

dpb
dpb on 1 Nov 2023
Edited: dpb on 1 Nov 2023
load carsmall
subplot(1,2,1)
gscatter(Displacement,Horsepower,Model_Year) % default appearance
subplot(1,2,2)
hGSC=gscatter(Displacement,Horsepower,Model_Year); % original but save handles
hGSC(matches({hGSC.DisplayName},'70')).Color='k'; % set year 70 group color to black

More Answers (2)

Dyuman Joshi
Dyuman Joshi on 1 Nov 2023
Edited: Dyuman Joshi on 1 Nov 2023
You can combine the other groups as one -
a = [37 98 1; 35 94 1; 33 97 1; 33 99 1; 31 96 1; 30 97 1; 29 100 1; 30 94 1; 31 90 1; 28 93 1; 21 63 1; 20 61 1; 22 60 1; 24 60 1; 24 64 1; 27 62 1; 28 65 1; 27 59 1; 23 57 1; 26 56 1; 27 57 1; 29 57 1; 31 60 1; 31 61 1; 31 64 1; 32 66 1; 36 60 1; 37 56 -1; 32 56 1; 31 54 1; 27 53 1; 13 43 2; 17 42 2; 17 40 2; 19 37 2; 14 40 2; 13 42 2; 10 41 2; 10 42 2; 9 39 2; 6 40 2; 25 20 3; 24 18 3; 26 18 3; 27 19 3; 27 21 3; 29 21 3; 30 22 3; 30 23 3; 31 26 3; 33 27 3; 35 28 3; 94 11 4; 95 10 4; 97 8 4; 94 8 4; 93 8 4; 99 12 4; 97 12 4; 98 15 4; 95 14 4; 91 15 4; 92 17 4; 91 19 4; 89 19 4; 87 21 4; 89 22 4; 86 23 4; 84 25 4; 81 25 4; 80 26 4; 82 26 4; 79 29 4; 82 29 4; 83 29 4; 86 28 4; 88 25 4; 91 24 4; 93 21 4; 94 22 4; 94 19 4; 95 18 4; 97 20 4];
g = a(:,3);
%Change the values of other groups to a random value that is not -1
g(g~=-1) = 3;
%To different groups
n=2;
%% Define parameters (I have taken values randomly)
%Colors
clr = [0 0 1; 1 0 0];
%Marker
m = [".", "*"];
%Marker size
ms = [12.5 5];
gscatter(a(:,1), a(:,2), g, clr, m, ms)
  3 Comments
Dyuman Joshi
Dyuman Joshi on 1 Nov 2023
Edited: Dyuman Joshi on 1 Nov 2023
@Sim, That can be adjusted as well -
a = [37 98 1; 35 94 1; 33 97 1; 33 99 1; 31 96 1; 30 97 1; 29 100 1; 30 94 1; 31 90 1; 28 93 1; 21 63 1; 20 61 1; 22 60 1; 24 60 1; 24 64 1; 27 62 1; 28 65 1; 27 59 1; 23 57 1; 26 56 1; 27 57 1; 29 57 1; 31 60 1; 31 61 1; 31 64 1; 32 66 1; 36 60 1; 37 56 -1; 32 56 1; 31 54 1; 27 53 1; 13 43 2; 17 42 2; 17 40 2; 19 37 2; 14 40 2; 13 42 2; 10 41 2; 10 42 2; 9 39 2; 6 40 2; 25 20 3; 24 18 3; 26 18 3; 27 19 3; 27 21 3; 29 21 3; 30 22 3; 30 23 3; 31 26 3; 33 27 3; 35 28 3; 94 11 4; 95 10 4; 97 8 4; 94 8 4; 93 8 4; 99 12 4; 97 12 4; 98 15 4; 95 14 4; 91 15 4; 92 17 4; 91 19 4; 89 19 4; 87 21 4; 89 22 4; 86 23 4; 84 25 4; 81 25 4; 80 26 4; 82 26 4; 79 29 4; 82 29 4; 83 29 4; 86 28 4; 88 25 4; 91 24 4; 93 21 4; 94 22 4; 94 19 4; 95 18 4; 97 20 4];
[g, q] = findgroups(a(:,3));
idx = q==-1;
n = numel(q);
%% Define parameters (I have taken values randomly)
%Colors
clr = hsv(n);
%Marker
m = repelem("*", 1, n);
m(idx) = ".";
%Marker size
ms = repelem(10, 1, n);
ms(idx) = 15;
gscatter(a(:,1), a(:,2), g, clr, m, ms)
Sim
Sim on 1 Nov 2023
ahhh okkk :-) I can switch the accepted answer.. I mean both of your answers, @Dyuman Joshi and @dpb, are equally great in my opinion.. I do not know who to grant the answer :-) :-)

Sign in to comment.


Sim
Sim on 1 Nov 2023
Edited: Sim on 1 Nov 2023
Thanks both @Dyuman Joshi and @dpb!
By mixing the @dpb and the @Cris LaPierre answers (that one from @Cris LaPierre is in Channing color, marker size and type in a gscatter), I got what I needed. I set all the properties for all the groups, and then I changed the properties for the group labelled with "-1":
% input
a = [37 98 1; 35 94 1; 33 97 1; 33 99 1; 31 96 1; 30 97 1; 29 100 1; 30 94 1; 31 90 1; 28 93 1; 21 63 1; 20 61 1; 22 60 1; 24 60 1; 24 64 1; 27 62 1; 28 65 1; 27 59 1; 23 57 1; 26 56 1; 27 57 1; 29 57 1; 31 60 1; 31 61 1; 31 64 1; 32 66 1; 36 60 1; 37 56 -1; 32 56 1; 31 54 1; 27 53 1; 13 43 2; 17 42 2; 17 40 2; 19 37 2; 14 40 2; 13 42 2; 10 41 2; 10 42 2; 9 39 2; 6 40 2; 25 20 3; 24 18 3; 26 18 3; 27 19 3; 27 21 3; 29 21 3; 30 22 3; 30 23 3; 31 26 3; 33 27 3; 35 28 3; 94 11 4; 95 10 4; 97 8 4; 94 8 4; 93 8 4; 99 12 4; 97 12 4; 98 15 4; 95 14 4; 91 15 4; 92 17 4; 91 19 4; 89 19 4; 87 21 4; 89 22 4; 86 23 4; 84 25 4; 81 25 4; 80 26 4; 82 26 4; 79 29 4; 82 29 4; 83 29 4; 86 28 4; 88 25 4; 91 24 4; 93 21 4; 94 22 4; 94 19 4; 95 18 4; 97 20 4];
% same properties for all the groups
number_groups = length(unique(a(:,3)));
color = hsv(number_groups);
markers = repelem('o',number_groups);
sz = repelem(10,number_groups);
g = gscatter(a(:,1),a(:,2),a(:,3),color,markers,sz);
for i = 1 : number_groups
g(i).MarkerFaceColor=g(i).Color;
end
% change properties only for the group labelled as "-1"
g(matches({g.DisplayName},'-1')).MarkerFaceColor='none';
g(matches({g.DisplayName},'-1')).MarkerEdgeColor='k';
g(matches({g.DisplayName},'-1')).Marker='^';
I think I would accept the @dpb answer since a bit closer to what I was looking for, but obviously, the @Dyuman Joshi answer is nice as well! :-)
  2 Comments
dpb
dpb on 1 Nov 2023
You can always vote for others that helped; Votes aren't restricted to picking only one...and, there often are multiple ways to skin the cat or as here pieces can be taken from various solutions so the one "right" answer isn't always a palatable choice, true...
Sim
Sim on 1 Nov 2023
Edited: Sim on 1 Nov 2023
Thanks! Yes, I voted both of you :-)

Sign in to comment.

Categories

Find more on Data Distribution 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!