group-lines与scatter-groups相同的颜色

时间:2016-03-09 15:07:17

标签: matlab colors grouping scatter-plot

我正在绘制具有不同颜色的分组散点图。我让Matlab决定颜色,并使用平面映射到(默认?)色图。

我需要使用相同颜色,就像在行的散点图组中一样。

但是,行的默认ColorOrder与散点图不同。因此,重置ColorOrderIndex并不能解决问题。

目前我可以制作以下内容:

enter image description here

使用:

% Data
N  = 3;
T  = 5;
xm = [6.3327    4.5682    6.1398
    7.6289    5.4423   10.5246
    3.5362    6.2185    8.2151
    6.6572    9.4543    7.4266
    6.1138    8.6453    8.2044];
ym = [7     0     7
    11     0    55
     1     1    18
    14     6    12
     8     2    22];
lm = [6.8176    0.3609    6.9693
   13.0347    0.5587   62.4217
    1.6841    0.8237   19.6712
    8.0186    4.1533   13.2623
    6.1108    2.7716   19.5666]

% Scatter groups
g = kron((1:N)',ones(T,1)); %// '
hs = scatter(xm(:),ym(:),[],g,'filled');
hold on

% Poisson group-fits
[xmsorted,posxm] = sort(xm);
posxm            = bsxfun(@plus, (0:N-1)*T, posxm);
hl               = plot(xmsorted,lm(posxm));

我试图从散点图中获取CDATA,它基本上是我的g(组索引),并使用它直接索引到默认的colormap,但似乎散点图使用不同的颜色映射?

1 个答案:

答案 0 :(得分:2)

似乎scatter缩放到图的colormap,而不是使用直接索引。所以如果cm = colormap;那么

round(1:((size(cm, 1) - 1) / (N - 1)):size(cm, 1))

应该返回colormap中的索引(参见this question)。默认colormap是'parula',因此如果scatter使用直接索引而不是缩放,则组将具有非常相似的颜色。

要使后续线图使用scatter的颜色,您可以使用

cm(round(1:((size(cm, 1) - 1) / (N - 1)):size(cm, 1)));

设置绘图的线条颜色。

或者,您可以更改图的colormap:假设颜色顺序是默认值(即新plot以自然顺序从lines获取颜色),

colormap(lines(N));

其中N是组的数量,应使scatter和线条颜色匹配。