Search code examples
matlabgraphmatlab-figure

Simple way to get more sensible colors in gscatter


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


Solution

  • 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.