Create colorbar for custom colormap used in Scatter?

29 views (last 30 days)
I am trying to create an (X,Y,Z) scatter plot, where Z is represented by color. I successfully did this by creating a custom color matrix (Mx3) where M is the length of X,Y and Z vectors, and each row is a 1x3 rgb vector. Each row is a color that has been calculated to represent to it's corresponding Z value.
if true
% scatter(Xvalues,Yvalues,[],colormatrix)
end
NOW, I would like to create a colorbar to give an idea of what numerical value goes with each color. I would also be satisfied with some sort of legend.

Answers (1)

Mike Garrity
Mike Garrity on 11 Aug 2015
Edited: Mike Garrity on 11 Aug 2015
Colorbar can't do this because you've already converted your data values into RGB. Since your scatter object doesn't have the original data values, colorbar can't see them.
You probably want to back up a bit. How did you generate colormatrix? It probably involved a color lookup. If you take the lookup table you used at that step and made that the colormap, then you could just pass your data values into scatter and colorbar would know how to display the lookup table.
Consider this example:
x = randn(1,100);
y = randn(1,100);
cdata = randi(10,[1,100]);
rgb_table = squeeze(hsv2rgb(repmat(.5,[10 1]),linspace(0,1,10)',repmat(1,[10 1])));
If I wanted to create a scatter with the points colored using the cdata, I could use cdata as an index into rgb_table to get the rgb values, or I could do this:
scatter(x,y,[],cdata,'filled')
colormap(rgb_table)
colorbar
Alternatively, if you want to leave your scatter the way you have it, you're basically going to need to lie to colorbar. The way you're going to do that is basically identical to what I just did above. You need to set the colormap to the lookup table you used, and you need to set the CLim of the axes to the range of your data.
Using legend would involve a similar sort of lie. You would create a set of invisible objects with the correct colors, and then insert them into the legend with their DisplayName properties set to the values.
  2 Comments
Kristin Fitzmorris
Kristin Fitzmorris on 11 Aug 2015
Thanks Mike
I'm afraid I'm not familiar with a "color lookup table" so I was a little lost in your explanation.
I tried using your code however, using my colormatrix instead of "rgb_table":
if true
% scatter(x,y,[],cdata,'filled')
colormap(colormatrix)
colorbar
end
It didn't work for me....everything was jumbled up in the colorbar: the right colors but not sequential. It is essentially stratified by color depending on the order of the variables (which was part of my struggle in the first place).
Let me clarify my process though: I created a function (input is data vector Z and 5 different rgb colors--one each for +/-2stdev, +/-1stdev and the mean), that uses a loop to assign colors depending on what standard deviation block each data point lies in. Anything outside +/-2sigma is red/green. The remaining data points are assigned colors that are calculated depending on their percentage distance between the two sigma boundaries of the sigma "bin" they fall into (using simple point-distance math).
e.g.
if true
for i=1:numel(data).....
if data(i)<=sigma1 && data(i)>sigma2
perc=abs(data(i)-sigma1)/sigma;
colori=(c2-c1).*perc + c1;
colormatrix=[colormatrix;colori]
else if data(i)<=mean && data(i)>sigma1
....etc.
end
end
where data is my vector of Z values, sigma1, sigma2, mean etc are the boundary values I mentioned before, c1 and c2 and the corresponding inputted rgb vectors for sigma1 and sigma2, and colormatrix is my resulting color matrix.
Any additional insight would be great; thanks!
Kristin Fitzmorris
Kristin Fitzmorris on 11 Aug 2015
I came up with a partial solution: just created an index vector using sort, then applied it to my colormatrix.
if true
% [~,I]=sort(data)
cmap=colormatrix(I,:)
colormap(cmap)
colorbar
end
Now the colors are appropriately in order, however the ticks don't match up properly, nor do they represent the correct bounds and values of "data" in the first place.

Sign in to comment.

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!