Simple way to get more sensible colors in gscatter
Asked Answered
M

2

2

I'm looking for a simple way to get gscatter to choose more sensible colors.

As you can see in the picture below, groups 3 and 4 have very similar colors, which are difficult to distinguish.

I'm plotting my data using gscatter(X(:,1),X(:,4),assigns , [], [] ).

I know I can use scatter to manually get more sensible colors by creating a colormap that has the same number of colors as the number of groups I have, but then how do I get a nice legend like gscatter produces without looping over each group?

So, is there a simple(r) way to get more sensible colors with gscatter?

Thanks.

enter image description here

Marileemarilin answered 17/3, 2015 at 19:39 Comment(0)
P
6

The fourth argument of gscatter is the color specification. According to the documentation, only letters can be used to define the colors:

gscatter(x,y,group,clr,sym,siz) specifies the color, marker type, and size for each group. clr is a string array of colors recognized by the plot function. The default for clr is 'bgrcmyk'.

But if you type open gscatter and look at the comments in the first lines (Matlab's old-style help), surprise!

GSCATTER(X,Y,G,CLR,SYM,SIZ) specifies the colors, markers, and
size to use. CLR is either a string of color specifications or
a three-column matrix of color specifications
.

So you can use a colormap matrix to define the colors you want (at least in Matlab R2014b).

Example:

load discrim
group(1:3:end) = 3; %// borrowing Benoit_11's idea to create two more groups
group(2:2:end) = 4;
cmap = hsv(4); %// define your colormap here
gscatter(ratings(:,1), ratings(:,2), group, cmap)

enter image description here


EDIT: In newer Matlab versions (I checked R2019a) the documentation does mention the possibility to specify the colors as a three-column matrix:

clr: Marker colors: character vector or string scalar of colors | matrix of RGB triplet values.

Pretender answered 17/3, 2015 at 22:58 Comment(3)
I'm accepting your answer for it is indeed simpler. +1 for you, -1 for undocumented features :-(Marileemarilin
@CassioPereira Well, in this case I wouldn't say they are undocumented features. It's just that they are documented in a hidden place :-)Pretender
@Benoit_11 Thanks! Your solution is actually very good too (+1 already)Pretender
M
4

Here is some kind of trade-off in which you do use gscatter and its nice legend functionality but you need to loop through each group to set a color manually, if you want.

The trick is to assign an output during the call to gscatter and afterward change the Color property. You can of course change any property you want.

In the simple example, I generate random colors for each group but you could easily access entries of a custom colormap with the colors you need.:

clear
clc
close all

load discrim

%// Just creating 2 more groups for the demo.
group(1:3:end) = 3;
group(2:2:end) = 4;

figure;

%// Retrieve handles of the scatter plot
hScatter = gscatter(ratings(:,1),ratings(:,2),group);

%// Set colors manually. You can use your own colormap.
for k = 1:numel(hScatter)
set(hScatter(k),'Color',rand(1,3))
end

xlabel('climate');
ylabel('housing');

Output:

enter image description here

Modification answered 17/3, 2015 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.