
Plot multiple variables in different colors with scatter3
조회 수: 9 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2017년 12월 16일
편집: Star Strider
2017년 12월 16일
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
Star Strider
2017년 12월 16일
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.
추가 답변 (2개)
Image Analyst
2017년 12월 16일
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
Image Analyst
2017년 12월 16일
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
2018년 4월 10일
편집: Julien Van der Borght
2018년 4월 10일

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
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!