3D Plot points as spheres instead of dots?
100 views (last 30 days)
Show older comments
I've created a 3D animation using the plot3 function and a loop, which works well enough. Currently the points are plotted as dots, but I want them to be spheres. I don't see any way to do this is the current list of linespec options. Can this be done? Currently I'm just using 'r.' for a red dot, but would like it to be a red sphere instead.
0 Comments
Answers (4)
Tyler Morrison
on 1 Oct 2021
Edited:Tyler Morrison
on 1 Oct 2021
I know its's a lot later now, but here is the simple function I whipped up today:
functionS = plotSpheres(x, y, z, r, color)
% Get current axes held state.
held = ishold;
% Hold the axes of the current plot so we append to it.
holdon
% Get how many points we are using and check the dimensions match.
Npts = numel(x);
assert(Npts == numel(y));
assert(Npts == numel(z));
% Divide the radius number. This helps keep it in the integer range.
r = r/100;
% Get the current axes scales. (Unforunately the spheres will stretch if
% the axes are resized later.)
ax = gca;
xscale = ax.XLim(2) - ax.XLim(1);
yscale = ax.YLim(2) - ax.YLim(1);
zscale = ax.ZLim(2) - ax.ZLim(1);
% Preallocate the graphics object array.
S = gobjects(Npts, 1);
% Loop through all the give points.
fori = 1:Npts
% Make a unit sphere.
[X, Y, Z] = sphere;
% Scale and shift the sphere.
X = X*r*xscale + x(i);
Y = Y*r*yscale + y(i);
Z = Z*r*zscale + z(i);
% Plot the sphere.
S(i) = surf(X, Y, Z);
%可视化设置。
set(S(i),'FaceColor', color);% Set color.
set(S(i),'EdgeColor','none');% Hide edges (wireframe).
set(S(i),'FaceLighting','gouraud');% Set fancy lighting.
end
% Return to the hold status that it initially had.
ifheld; holdon;else; holdoff;end
end
0 Comments
Chad Greene
2015年11月15日
Another option using built-in commands:
[x,y,z] = sphere(30);
s1 = surf(x,y,z);
shadinginterp
holdon
axisequal
camlight
lightingphong
set(s1,'facecolor',[.98 .45 .02]);
Walter Roberson
on 16 Nov 2015
After you have created whatever line you need with plot3()
without
a marker, use scatter3() to plot the markers. scatter3() with 'filled' creates filled spheres.
3 Comments
Ben Ward
on 24 Jan 2019
They are not really spheres, more like circles that always face directly to the camera. If you have a marker that bisects a plane, the intersection shows up as a straight line. If they were spheres the intersection would not be straight. This behaviour is a bit unfortunate, because the markers are more easily obscured than they would be if truly spherical.
It wouldn't be a problem if it was possible to specify the orientation of the circles in 3D space (i.e. along the x, y or z axis).
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!