coloring by third variable

8 views (last 30 days)
jenka
jenka on 6 Feb 2017
Edited: dpb on 6 Feb 2017
Dear All, quick question. I have three variables comb(:,1), comb(:,2), comb(:,3) where comb(:,3) is the variable of either 0s or 1s. I would like to plot(comb(:,1), comb(:,2)) but color it by comb(:,3). The code below appears to work but it does give me different coloring depending on subplot. For example, if comb(:,3) are all 0 the circle will be blue at one subplot. If comb(:,3) is the mix of 0/1s it will give me 0s that are not blue but of some other color. But I would like to keep it consistent. Is it possible?
subplot(2,3,kk)
kk = kk + 1;
plot(comb(:,1), comb(:,2),'or');
hold on;
plot(comb(:,1), comb(:,2),'r');
datetick('x','HH:MM:SS PM','keeplimits');
set(gca,'XLim',[min(comb(:,1)),max(comb(:,1))]);
title(cat(2,'Mud Temp for GPM:',num2str(unique(GPM(index_arr{i})))));
scatter(comb(:,1), comb(:,2),20, comb(:,3),'filled');

Answers (1)

dpb
dpb on 6 Feb 2017
Edited: dpb on 6 Feb 2017
Sure, just use a set of RGB triplets for the color vector instead of the values. Each row of the matrix specifies an RGB triplet color for the corresponding marker. Obviously the length of the array then must be same as length of data vectors.
It would be much appreciated enhancement if TMW would allow for the use of a vector of color strings instead of requiring the RGB triplet, but the RGB array does get the job done. (The color string can only be a single value to set all markers the same color.)
doc scatter % for details under the input for _C_, 'Marker Color'
  2 Comments
Steven Lord
Steven Lord on 6 Feb 2017
Alternately use a vector of values for the c input to scatter and ensure all the subplots have the same colormap (or that none of the subplots have a colormap and set one on the figure.)
The idea to have scatter accept a string vector of color names is an interesting one, though if you were allowed to specify full names it could chew up memory for large data sets. I'd say that's worth submitting as an enhancement request to Technical Support.
dpb
dpb on 6 Feb 2017
Edited: dpb on 6 Feb 2017
I'd presume that folks would generally stick with the one-letter shorthand altho for consistency guess it would be essentially mandatory to make full names allowable as well.
ADDENDUM
Although if one uses default values in an RGB triplet (say rgb=[1 0 0] for just a one-element plot as the minimum), rgb will be a 3-vector of double and hence 3x8=24 bytes whereas the character string array would be just 7 bytes. Now, cellstr is terribly overhead-expensive, indeed. Experimenting in placing the eight named colors into a cell array in order of decreasing length, the memory usage vis a vis characters entered each step is
>> [ccnt;l]
ans =
74 146 216 286 356 424 492 558
7 6 5 5 5 4 4 3
>>
A little spelunking leads us to discover that
>> diff([0 ccnt])-2*l
ans =
60 60 60 60 60 60 60 60
>>
from which we conclude there's an overhead of 60(!) bytes per element plus the characters are stored as 2-bytes rather than just one.
If the feature were to be implemented, that overhead could be significant being something nearly 3X the cost of the 3-vector of doubles.
As an aside, I've noticed the high memory overhead of cell arrays in the past and wondered if there surely weren't a more efficient implementation possible.
I'm pretty sure I've submitted this in the (fairly distant) past, Steven; you're more than free to pick it up if wish... :)

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!