How do I make a 3d scatter plot?

7 visualizaciones (últimos 30 días)
jacob smith
jacob smith el 18 de Sept. de 2016
Comentada: Star Strider el 18 de Sept. de 2016
I keep getting an error message about "vector not same length"
function [ output_args ] = scat( xstart,ystart )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
n=3;
for m=0:n
x=xstart+1*m;
y=ystart+1*m;
xax=xstart:0.5:xstart+n*0.5
yax=ystart:0.5:ystart+n*0.5
end
for m=1:n+1
z(m)=m;
end
figure
plot([xax,yax],z,'.')
end
I am trying to create a very easy scatter plot. Is there also a a way to look at the cross-section of the 3d scatter plot?

Respuestas (3)

mahesh babu
mahesh babu el 18 de Sept. de 2016
Editada: mahesh babu el 18 de Sept. de 2016
try using fplot instead of plot
fplot([xax,yax],z,'.')
  4 comentarios
mahesh babu
mahesh babu el 18 de Sept. de 2016
try pasting this, if you still get the error, then remove xstart and ystart and replace them with constants
jacob smith
jacob smith el 18 de Sept. de 2016
It doesn't work and it kinda defeats the purpose of the user inputting the parameters xstart and ystart.
Also I would prefer to leave the first loop iterating by integers.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 18 de Sept. de 2016
You have
for m=0:n
x=xstart+1*m;
y=ystart+1*m;
xax=xstart:0.5:xstart+n*0.5
yax=ystart:0.5:ystart+n*0.5
end
None of your variables x, y, xax, yax, are indexed at m (or a value derived from m), so each loop through you are overwriting all of the previous values. The effect is exactly the same as if you had only done the last iteration,
m = n;
x=xstart+1*m;
y=ystart+1*m;
xax=xstart:0.5:xstart+n*0.5
yax=ystart:0.5:ystart+n*0.5
It seems unlikely that is what you want.
For a 3D scatter plot, you should be using scatter3(), something similar to
scatter3(xax, yax, z)

Star Strider
Star Strider el 18 de Sept. de 2016
If you have three arguments, you need to substitute plot3 for plot.
Try this:
function [ output_args ] = scat( xstart,ystart )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
n=3;
for m=0:n
x=xstart+1*m;
y=ystart+1*m;
xax=xstart:0.5:xstart+n*0.5;
yax=ystart:0.5:ystart+n*0.5;
end
for m=1:n+1
z(m)=m;
end
figure
plot3(xax,yax,z,'.')
grid on
end
With my changes, the internal part of your code runs for me without error, and produces the plot. I leave it to you to determine if it produces the correct result.
Note that you do not assign ‘output_args’ in your function, so that will throw an error:
Error in [calling script] (line [line #])
[ output_args ] = scat( xstart,ystart )
Output argument "output_args" (and maybe others) not assigned during call to
"ScratchPad/scat".
You have to decide what you want your ‘scat’ function to return.
  2 comentarios
jacob smith
jacob smith el 18 de Sept. de 2016
How does one rotate the 3d plot or look at a cross section of the 3d plot?
Star Strider
Star Strider el 18 de Sept. de 2016
You can do it manually in the plot GUI, or if you already know the orientation you want, use the view function. (It’s easier to do it with the plot GUI first, then later fix the view in your program with the view function. That’s what I usually do. In the plot GUI, the azimuth and elevation are in the lower left corner of the plot figure.)

Iniciar sesión para comentar.

Categorías

Más información sobre Annotations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by