3D scatter plot with different color and legend explaining each color.

Hi,I want to draw a 3D scatter plot with different colors and want to put a legend that explains what each color means.
My example is something like this.
b's are the data points that I want to put in the 3D scatter. b1=[1,2,3,4,5] b2=[2,2,3,3,1] b3=[1,5,4,4,3]
s is the size of the points s=[1,1,1,1,1]
r is the color of the points (three colors) r=[0,0,2,1,2]
Finally the scatter plot is this = scatter3(b1,b2,b3,s,r,'filled')
My questions are. 1)Can I set a designate color to each numbers in r? for example, red for 0, blue for 1, green for 2.
2) how can I set a legend that explains what the color means? for example. for color 0 - 'efficient', for color 1 - 'inefficient', for color 2- 'not an eq'

2 Comments

Thanks for asking this Jinsoo.
My question is similar, and I will try Image Analyst's code below.
CHALLENGE: I have three column vectors of scalars, to plot the results of a Principal Components Analysis on multiple (41) individuals. These I send to a stem3D plot, with size and uniform color. Simple. I want to code individual plot elements in (6) different colors, corresponding to intervals of an index [which corresponds very nicely to one of the PC axes, was I hoped.]
I observe that Image Analyst's code will color points as successive blocks of defined size, which I could do for my data if I sorted my data matrix by index, identify the number of points in each block, write the customColorMap as below, and bobs your uncle. HOWEVER, this would be data matrix specific.
What I would like (and have tried to do) is to supply an additional column vector, where each element is the appropriate colorcode for the individual. [Essentially a discrete heat map]. Adding additional individuals (or removing outliers experimentally) is then simple.
MatLab doesn't like any of my vector formats of the form [0 0 1] etc or otherwise to try this. My two reference books don't address the problem.
Advice appreciated.
@Steve Carr, you can make up a list of colors to apply to each marker, however it's not a column vector. It's an N-by-3 matrix of colors in the 0-1 range. If you have a column vector of something, like counts or frequency, you can turn that scalar column vector into a list of colors easily by just using the value as the row index into your colormap.
numColors = 256;
colorIndex = round(rescale(yourColumnVector, 1, numColors));
cmap = jet(numColors); % Whatever one you want.
markerColors = cmap(colorIndex, :);
markerSize = 100;
% Create the scatter plot.
scatter3(x, y, z, markerSize, markerColors, 'filled')
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('Z', 'FontSize', fontSize);
If you still have trouble, attach your data in a new question.

Sign in to comment.

 Accepted Answer

Try this:
fontSize = 20;
numPoints = 30;
b1 = randi(9, 1, numPoints);
b2 = randi(9, 1, numPoints);
b3 = randi(9, 1, numPoints);
% markerSize is the size of the markers.
markerSize = 100;
% customColorMap is the color of the points (three colors)
% Option 1: Use the built-in "jet" colormap.
% customColorMap = jet(numPoints);
% Option 2: Make up a custom one.
customColorMap = zeros(numPoints, 3); % Initialize.
% Make the first 10 points red
customColorMap(1:10, :) = repmat([1, 0, 0], 10, 1);
% Make the next 10 points dark green
customColorMap(11:20, :) = repmat([0, 0.6, 0], 10, 1);
% Make the remaining points blue
customColorMap(21:end, :) = repmat([0, 0, 1], 10, 1);
% Create the scatter plot.
scatter3(b1, b2, b3, markerSize, customColorMap,'filled')
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('Z', 'FontSize', fontSize);

7 Comments

Thank you. Now I understand that Matlab specify with three numbers. I accepted your answer. Could you also tell me how to put an legend for each color? Red-inefficient, blue-efficient, Green- not an eq
I'm not sure. The legend function doesn't seem to do that. You can use text:
text(1, 1, 'Red-inefficient, Blue-efficient, Green- not an eq');
Use sprintf() to put the markers into the text string if you want them. Use the 'ForegroundColor' option if you want multiple text in different colors.
I would like to add 5 colors - I add a color every 5 rows, but I have a problem with the size of the right side
numPoints = 25;
KolorMap = zeros(numPoints, 5); % Initialize.
KolorMap(1:5, :) = repmat([0.450, 0.419, 0.980], 5, 1);
KolorMap(6:10, :) = repmat([0.305, 0.270, 0.890], 5, 1);
KolorMap(11:15, :) = repmat([0.098, 0.058, 0.760], 5, 1);
KolorMap(16:20, :) = repmat([0.207, 0.184, 0.596], 5, 1);
KolorMap(21:25, :) = repmat([0.086, 0.066, 0.435], 5, 1);
And I have error:
Unable to perform assignment because the size of the left side is 5-by-5 and the size of the right side is 1-by-3.
I read a doc for repmat but I don't understand it..
x, y and z to scatter3 I have already defined and works.
And the other questions: Is it possible to change markertype, that there were 5 types in? Because I want that x change in color, y change in markertype, z is neutral
Thank you for any answers!
You need to initialize to 3 columns, not 5. But it's better to use repelem():
numPoints = 25;
KolorMap = zeros(numPoints, 3); % Initialize with 3 columns.
KolorMap(1:5, :) = repmat([0.450, 0.419, 0.980], 5, 1); % Replicate row 5 times.
KolorMap(6:10, :) = repmat([0.305, 0.270, 0.890], 5, 1); % Replicate row 5 times.
KolorMap(11:15, :) = repmat([0.098, 0.058, 0.760], 5, 1); % Replicate row 5 times.
KolorMap(16:20, :) = repmat([0.207, 0.184, 0.596], 5, 1); % Replicate row 5 times.
KolorMap(21:25, :) = repmat([0.086, 0.066, 0.435], 5, 1) % Replicate row 5 times.
% The better way, using the new repelem() function:
colors = [0.450, 0.419, 0.980; % Initialize colors - one row per color.
0.305, 0.270, 0.890;
0.098, 0.058, 0.760;
0.207, 0.184, 0.596;
0.086, 0.066, 0.435]
% Replicate each row 5 times.
KolorMap = repelem(colors, 5, 1)
Unfortunately I can't deal with this problem. When I import data without changing colors it works, but when it indicates colors - S must be a scalar or a vector the same length as X ..
But my vectors look the same as those from your example, except there are 25 :(
Can I ask a new question in the forum regarding this question, will it be considered a thread duplication and closed? I can't solve this problem myself ...

Sign in to comment.

More Answers (2)

Yes, you can specify each color for every row of r. Just go ahead and make up your N by 3 r color array. For example
r = jet(length(b1));
or whatever....
By the way, you might also like to check out the function called gscatter in the stats toolbox.

1 Comment

Hi, Thank you for your reply. However, I don't understand your answer since I am a beginner in Matlab. could you give me more explanation for my question? I also need to put a legend to explain what the color means.

Sign in to comment.

Since no one else answers except the one I can't understand, I will just close this question by myself

Community Treasure Hunt

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

Start Hunting!