How do I make a 3d scatter plot?

7 次查看(过去 30 天)
jacob smith
jacob smith 2016-9-18
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?

回答(3 个)

mahesh babu
mahesh babu 2016-9-18
编辑:mahesh babu 2016-9-18
try using fplot instead of plot
fplot([xax,yax],z,'.')
  4 个评论
mahesh babu
mahesh babu 2016-9-18
try pasting this, if you still get the error, then remove xstart and ystart and replace them with constants
jacob smith
jacob smith 2016-9-18
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.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2016-9-18
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 2016-9-18
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 个评论
jacob smith
jacob smith 2016-9-18
How does one rotate the 3d plot or look at a cross section of the 3d plot?
Star Strider
Star Strider 2016-9-18
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.)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by