
Plot multiple variables in different colors with scatter3
10 views (last 30 days)
Show older comments
Aulia Pramesthita
on 16 Dec 2017
Edited: Julien Van der Borght
on 10 Apr 2018
Hi,
I have to plot a coordinate (x,y,z).
The result of my project is 20 coordinates. I want to plot these coordinates in a 3D graph using scatter3(), or any other function, where I need to plot each coordinate in a different color.
For example:
I want to plot (x1,y1,z1) in red, (x2,y2,z2) in yellow, (x3,y3,z3) in blue.
Something like that.
But I am unable to plot these with different colors in the same graph.
Please help!
Thanks in advance!
0 Comments
Accepted Answer
Star Strider
on 16 Dec 2017
Edited: Star Strider
on 16 Dec 2017
Try this:
x1 = randi(9, 5, 1);
y1 = randi(9, 5, 1);
z1 = randi(9, 5, 1);
x2 = randi(9, 5, 1);
y2 = randi(9, 5, 1);
z2 = randi(9, 5, 1);
figure(1)
scatter3(x1, y1, z1, 'r', 'filled')
hold on
scatter3(x2, y2, z2, 'y', 'filled')
hold off
grid on
legend('x_1', 'x_2')
Do the same for the rest.

4 Comments
Star Strider
on 16 Dec 2017
You have to plot each vector individually with scatter3.
If your ‘x1’, ‘y1’ and ‘z1’ are column vectors stored in matrix ‘M1’ for example, as:
M1 = [x1, y1, z1];
you would plot them as:
scatter3(M1(:,1), M1(:,2), M1(:,3), 'r')
to plot ‘x1’, ‘y1’, and ‘z1’ in red.
Here the color argument 'r' tells scatter3 to plot them in red. Change that to the color you want in the other plots to plot each set in different colors, for example 'y' for yellow and 'b' for blue.
More Answers (2)
Image Analyst
on 16 Dec 2017
Try this:
% Create sample data.
numPoints = 100; % Will be 20 in your case.
t = linspace(0, 4*pi, 100);
x = sin(t);
y = cos(t);
z = t/50;
% Now plot each point in a different color.
% First create a list of colors - one unique color for each point.
markerColors = hsv(length(x))
% Now do the scatterplot.
scatter3(x, y, z, 50, markerColors, 'filled')
grid on

9 Comments
Image Analyst
on 16 Dec 2017
Never mind. I see you accepted Star's answer so I guess you had multiple sets of x, etc. (e.g. three vectors x1, x2, and x3) rather than just a single set (x) like I had assumed. His code will plot each set of points in the specified color. So 3 sets with one unique color for all the points in each set, like all points in set 1 in red, all points in set 2 in yellow, and all points in set 3 in blue.
Sorry I misinterpreted your original post.
Julien Van der Borght
on 10 Apr 2018
Edited: Julien Van der Borght
on 10 Apr 2018

Hello, I have a linked question to this one. I have a vessel newtork created by Skeleton3D that I applied to my dataset. I obtain the figure that you see here with the following command: scatter3(y,x,z,3,4*s,'filled'); The colormap define the vessel diameter in the network (in micro-meter) Now, I want to emphasize the distinction between small vessel and medium one, so that the biggest one are painted in red and the other one in blue. Can you help me in this way? Thank you!
0 Comments
See Also
Categories
Find more on Discrete Data Plots 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!