How to set for each point a different color

Hello,
How do I set the color for points by plot,
I want to have for example 1000 points, each point is different color.
Thanks.

 Accepted Answer

You can use scatter and pass in the color for each point. See this demo:
% Demo to very color of scatterpoints depending on their location
clc;
clear
x = rand(1000,1);
y = rand(1000,1);
distances = ((x-.5).^2 + (y-0.5).^2).^0.5;
% Normalize - divide my sqrt(maxX^2 + maxY^2)
distances = distances / sqrt(.5^2 + .5^2);
[sortedDistances sortIndexes] = sort(distances);
% Arrange the data so that points close to the center
% use the blue end of the colormap, and points
% close to the edge use the red end of the colormap.
xs = x(sortIndexes);
ys = y(sortIndexes);
cmap = jet(length(x)); % Make 1000 colors.
scatter(xs, ys, 10, cmap, 'filled')
grid on;
title('Points where color depends on distance from center', ...
'FontSize', 30);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

7 Comments

thats really annoying, can't we just have a list that each index corresponds to the color?
If you do have such a list, then you can
numcolors = length(unique(colorindex));
cmap = jet(numcolors);
pointsize = 10;
scatter(x(:), y(:), pointsize, colorindex(:), 'filled');
colormap(cmap);
You can construct cmap any way you want; in particular you can assign particular values into it, such as
cmap = [1 0 0;
0 1 0;
1 0 1;
0 0 1];
Why do you say that it's annoying? You are annoyed by scatter()?
And you say "can't we just have a list that each index corresponds to the color?" Well, just what do you think cmap in my code is? It is exactly that, just like you wanted. So what is annoying?
Image Analyst,
Your cmap in the code was 1000 x 3, distinct colors for every point, instead of one color per unique index value in a list.
I guess I don't understand then. I had 1000 points. And I made up 1000 unique colors. And I passed in the points and the colors into scatter, which will plot point 1 in color 1, point 2 in color 2, ... and point 1000 in color #1000. The question was "How to set for each point a different color" and I believe I did that. Brando wanted "a list that each index corresponds to the color". Well, each row (the row number is the index) in cmap is a single, unique color and each color only applies to one point. Plus, no points share the same color, every point has a unique color. So what am I misunderstanding?
Brando is not the initial poster, and is asking about something slightly different that does not require the sorting and so on -- a way to be able to specify a color index for each point. Which is what already effectively happens if you pass a vector of values for color information.
Reflecting back, though, the code I posted does not truly treat the index vector as absolute indices into the color map. To do that with the colormap that is in effect, you would need:
numcolors = size(colormap(),1);
pointsize = 10;
scatter(x(:), y(:), pointsize, colorindex(:), 'filled');
caxis([1 numcolors])
assuming the index starts from 1 (which might not be the case for uint8 index information.)
Sorting was not necessary in my code. I just did that because it make the colors progress with distance in a pretty way. You could omit the sorting and just have unique colors randomly all over the place. You can make up cmap however you want. You don't have to use jet() or one of the other built in functions, you could make it up according to your own formula. You could even use the color map editor if you want. I still don't see why it's annoying or overly complicated.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!