Create colorbar for custom colormap used in Scatter?
29 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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
See Also
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!