0

I am plotting a grouped scatter plot with different colors. I let Matlab decide the colors, and it uses a flat mapping into the (default?) colormap.

I need to use the same colors as in the scatter-group for the lines.

However, the default ColorOrder of the lines is not the same as scatter's. Hence, resetting the ColorOrderIndex does NOT solve the issue.

Currently I can produce the follwing:

enter image description here

with:

% 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));

I tried to get the CDATA from the scatter, and it's basically my g (group index), and used that to index directly into the default colormap, but it seems that scatters use a different colormap?

Oleg
  • 10,406
  • 3
  • 29
  • 57
  • 2
    Your example requires toolboxes. Do any of [these](http://stackoverflow.com/questions/30183701/reset-colororder-index-for-plotting-in-matlab-octave) help? [Luis' answer with the `'ColorOrderIndex'` axes property](http://stackoverflow.com/a/30185215/2748311) may be the simplest. – sco1 Mar 09 '16 at 15:17
  • @excaza Thanks for pointing out the tb dependency, I hardcoded the data. Thats exactly what I was saying about scatter using a different colormap, resetting the ColorIndex has no effect. – Oleg Mar 09 '16 at 15:28
  • 1
    To those who marked duplicate: I do not think this is a duplicate of the linked question. This has nothing to do with `ColorOrderIndex`. – zeeMonkeez Mar 10 '16 at 18:16

1 Answers1

2

It seems scatter scales to the colormap of the figure, rather than using direct indexing. So if cm = colormap; then

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

should return the indices within the colormap (see this question). The default colormap is 'parula', so if scatter used direct indexing rather than scaling, groups would have very similar colors.

To make subsequent line plots use the colors of the scatter, you can use

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

to set the line colors for plotting.

Alternatively, you can change the colormap of the figure: assuming the color order is the default (i.e. new plots get colors from lines in natural order),

colormap(lines(N));

where N is the number of groups, should make scatter and line colors match.

Community
  • 1
  • 1
zeeMonkeez
  • 5,057
  • 3
  • 33
  • 56
  • The solution is valid. Not following what you mean by: "this makes sense as with the default colormap of parula direct indexing would lead to very similar colors". Would also be nice to find a way to inherit colors from scatter and pass them to lines, instead of changing. – Oleg Mar 09 '16 at 16:53
  • @Oleg I clarified (I hope) and added code for how to get the colors from scatter. – zeeMonkeez Mar 09 '16 at 17:37