MATLAB Answers

plot spheres in 3D with one color (no gradient)

312 views (last 30 days)
Hi everyone
I want to plot spheres in 3 D with one color (no gradient). I used surf function as shown below, where a is radius, XT,YT,ZT are the center position in x,y,z and N is number of spheres.
for k=1:1:1%size(XT,1)
for j=1:N
% Generate a sphere consisting of 20by 20 faces
[x,y,z]=sphere;
% use surf function to plot
hSurface=surf(a*x+XT(k,j),a*y+YT(k,j),a*z+ZT(k,j)); hold on
set(hSurface,'FaceColor',[0 0 1], 'FaceAlpha',0.5,'EdgeAlpha', 0);
axis([-0.5 0.5 -0.5 0.5 -0.5 0.5]);
daspect([1 1 1]);
W(k)=getframe(gcf);
end
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
end
movie2avi(W,'35_percent.avi');
Without using 'EdgeAlpha', the spheres look right in 3D but it is visible the point to point edges due to surf function. However, if I include 'EdgeAlpha' to make invisible point to point edges, but the spheres are now in Circle (2D).
How do I plot spheres in 3D and in one color without seeing the point-point edges of plotted surface.
Thanks

Accepted Answer

Mike Garrity
Mike Garrity on 5 Apr 2016
A solid FaceColor alone isn't going to give you any feeling of the 3D shape of the spheres. If you take the edges away, you'll need to use something else to show the shape.
One option is lighting:
N = 15;
a = 1/8;
XT = randn(1,N)/6;
YT = randn(1,N)/6;
ZT = randn(1,N)/6;
for j=1:N
% Generate a sphere consisting of 20by 20 faces
[x,y,z]=sphere;
% use surf function to plot
hSurface=surf(a*x+XT(j),a*y+YT(j),a*z+ZT(j));
hold on
set(hSurface,'FaceColor',[0 0 1], ...
'FaceAlpha',0.5,'FaceLighting','gouraud','EdgeColor','none')
axis([-0.5 0.5 -0.5 0.5 -0.5 0.5]);
daspect([1 1 1]);
end
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
camlight
Another option would be to use FaceColor to shade them:
N = 15;
a = 1/8;
XT = randn(1,N)/6;
YT = randn(1,N)/6;
ZT = randn(1,N)/6;
[x,y,z]=sphere;
l = [.25 -.433 .5];
c = max(0,x*l(1) + y*l(2) + z*l(3));
for j=1:N
% use surf function to plot
hSurface=surf(a*x+XT(j),a*y+YT(j),a*z+ZT(j),c);
hold on
set(hSurface,'FaceColor','interp','FaceAlpha',0.5,'EdgeColor','none')
axis([-0.5 0.5 -0.5 0.5 -0.5 0.5]);
daspect([1 1 1]);
end
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
colormap hot

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!