Plot multiple variables in different colors with scatter3

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!

 채택된 답변

Star Strider
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

You are plotting with x1,y1,z1 then x2,y2,z2. How about [x,y,z]?
‘Yes I already try your code, then modify it to my program. It's only plot with one color. I don't know where is wrong.’
You have to specify a different color for each set of variables, as I do here.
Did you run my code to see how it works? I added the scatter3 plot it produces to demonstrate that the colors are what you want for the first two sets of variables.
To add more plots, the scatter3 call becomes:
figure(1)
scatter3(x1, y1, z1, 'r', 'filled')
hold on
scatter3(x2, y2, z2, 'y', 'filled')
scatter3(x3, y3, z3, 'b', 'filled')
hold off
grid on
legend('x_1', 'x_2', 'x_3')
This will work for all your data.
Aulia   Pramesthita
Aulia Pramesthita 2017년 12월 16일
편집: Aulia Pramesthita 2017년 12월 16일
Oh I see. So, it can't work if I do it with [x,y,z]?
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
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

Thank you before.
I'm new in MATLAB, I don't know how to plot if my coordinates are in matrix? I have this code. The result of matrix [x,y,z] is 20 coordinates x,y,z. I want to give identity to each coordinates with different colors.
for i= 1:20
[x,y,z] = multilaterasi(Reader(1, 1), Reader(1, 2), Reader(1, 3), Distance(1,i), Reader(2, 1), Reader(2, 2), Reader(2, 3), Distance(2,i), Reader(3, 1), Reader(3, 2), Reader(3, 3), Distance(3,i), Reader(4, 1), Reader(4, 2), Reader(4, 3), Distance(4,i));
scatter3(x, y, z, 'MarkerEdgeColor', [1 0 0], 'MarkerFaceColor', [1 1 0]);figure(gcf)
end
Please help.
Then try the scatter3 function call I gave instead of the one you've used.
already try it, but the plot only have one color.
Try the code in my Answer.
Yes I already try your code, then modify it to my program. It's only plot with one color. I don't know where is wrong.
[x,y,z] = multilaterasi(Reader(1, 1), Reader(1, 2), Reader(1, 3), Distance(1,i), Reader(2, 1), Reader(2, 2), Reader(2, 3), Distance(2,i), Reader(3, 1), Reader(3, 2), Reader(3, 3), Distance(3,i), Reader(4, 1), Reader(4, 2), Reader(4, 3), Distance(4,i));
markerColors = hsv(length(x));
scatter3(x, y, z, 20, markerColors, 'filled')
grid on
Is it like this?
You have to specify a different color for each set of variables.
Did you run my code to see how it works? I added the scatter3 plot it produces to demonstrate that the colors are what you want for the first two sets of variables.
Aulia, Star and I are making different assumptions. I'm assuming that all 20 (x,y,z) points/locations are in one x array, one y array, and one z array. Star is assuming that some of the 20 (say, 5 of the points) are in an array called x1, some more (say, 7 of them) are in an array called x2, and some more in an array x3, etc. So, which is it? It looks like from your line:
[x,y,z] = multilaterasi(...........
that all 20 are in single arrays.
The code I gave you, and which you asked about in your latest comment will work, assuming all 20 points are in those arrays. Each of the 20 points will have a different color. Of course, if you'd prefer, you can specify more exactly the colors you want, rather than using a built-in colormap like hsv().
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.
Yes I'm using
[x,y,z]=multilaterasi(....
The result is in one coloumn matrix, for example:
x = 20
y = 23
z = 12
x = 13
y = 22
z = 13
until I get 20 x, y, and z.
I'm trying both of your code and Star's code. I'm already trying the code that you gave. But the result are in one color. So, I'm using the code that Star gave.
If you have the shortest solution than plot each variable, I will really appreciate that.
The main of my question is actually giving identity to each coordinate (I think to distinguish it by color).
I also apologize if my question is not clear. Thankyou anyway.

댓글을 달려면 로그인하십시오.

Julien Van der Borght
Julien Van der Borght 2018년 4월 10일
편집: Julien Van der Borght 2018년 4월 10일

0 개 추천

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!

카테고리

도움말 센터File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by