Simple way to get more sensible colors in gscatter

感情迁移 提交于 2019-12-24 03:29:59

问题


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.


回答1:


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)


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.




回答2:


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:



来源:https://stackoverflow.com/questions/29108196/simple-way-to-get-more-sensible-colors-in-gscatter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!