3D Plot points as spheres instead of dots?

125 Ansichten (letzte 30 Tage)
Aaron Mears
Aaron Mears am 15 Nov. 2015
Bearbeitet: Tyler Morrison am 1 Okt. 2021
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.

Antworten (4)

Tyler Morrison
Tyler Morrison am 1 Okt. 2021
Bearbeitet: Tyler Morrison am 1 Okt. 2021
I know its's a lot later now, but here is the simple function I whipped up today:
function S = 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.
hold on
% 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.
for i = 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);
% Visualization settings.
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.
if held; hold on; else; hold off; end
end

Chad Greene
Chad Greene am 15 Nov. 2015
You might be able to do it with the bubblegum plot function on FEX.

Chad Greene
Chad Greene am 15 Nov. 2015
Another option using built-in commands:
[x,y,z] = sphere(30);
s1 = surf(x,y,z);
shading interp
hold on
axis equal
camlight
lighting phong
set(s1,'facecolor',[.98 .45 .02]);
  3 Kommentare
Chad Greene
Chad Greene am 15 Nov. 2015
Yes, you'll have to scale the size of the sphere to how big you want it. You scale the sphere by multiplying x, y, and z by some value to make it bigger or smaller. You'll also have to add some displacement dx, dy, and dz values to place the center of the sphere to wherever you want it.
Aaron Mears
Aaron Mears am 15 Nov. 2015
Bearbeitet: Walter Roberson am 16 Nov. 2015
I'm not really sure how it would correlate to how I've already set this up. This is what I am doing:
p = plot3(0,0,0,'r.',x(j),y(j),z(j),'b.',a(j),b(j),c(j),'g.',d(j),e(j),f(j),'y.','markersize',20);
That is in a for loop, among other things that plots the points as it goes through the loop. It doesn't hold each last position, so it gives the plot an animated look as it iterates through the loop. The first point in that code above is a fixed point, the others are functions specified elsewhere in the code that are time based. I don't understand how to take what you've posted and apply it to my function in a way that it converts the plotted point to a sphere.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 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 Kommentare
Walter Roberson
Walter Roberson am 16 Nov. 2015
scatter3() creates spheres. They are not lighted or shaded but they are spheres.
If you want you can use
function SpheresAt(x,y,z,R)
if ~isvector(x) || ~isvector(y) || ~isvector(z) || ~isvector(R) || length(x) ~= length(y) || length(x) ~= length(z) || (length(R) > 1 & length(x) ~= length(R))
error('all inputs must be scalar or vector and compatible sizes')
end
if length(R) == 1
R = repmat(R, length(x), 1);
end
NumSphFaces = 30;
[SX,SY,SZ] = sphere(NumSphFaces);
washeld = ishold();
hold on;
for K = 1 : length(x)
surf(SX*R(K) + x(K), SY*R(K) + y(K), SZ*R(K) + z(K));
end
lighting phong
if ~washeld
hold off
end
Ben Ward
Ben Ward am 24 Jan. 2019
Bearbeitet: Ben Ward am 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).

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by