make slices and project to 2d plot
显示 更早的评论
Hello. I have a sphere that I want to make slices of. I want to be able to slice the sphere from the z axis at any point and obtain a 2d circle plots on the x, y axis. The slice at z = 0 would be the biggest circle and z = 4 would be smallest circle. How can I do this? Thank you.

2 个评论
darova
2021-4-2
Can't you calculate radius of a circle and just use plot3?
Nne Akallu
2021-4-8
回答(1 个)
The general idea would be to use contour()
contour(X,Y,Z,V) will draw the contour at each level specified by the vector V.
If you want to only specify one level, specify it as [1 1]*mylevel (i.e. V is a minimum of 2 elements)
Bear in mind, contour just throws the plot at Z=0. If you don't need your surf plot centered, you can always offset it. You can probably offset the zlabels to match. Off the top of my head, idk how you'd offset the contour plot.
[X,Y,Z]=sphere;
r=5;
X2=X*r;
Y2=Y*r;
Z2=Z*r;
% say i want to offset it
surfoffset=5;
clf
surf(X2,Y2,Z2+surfoffset); hold on
% fudge the z axis labels
osz=@(x) num2str(str2num(x)-surfoffset);
set(gca,'zticklabels',cellfun(osz,get(gca,'zticklabels'),'uniformoutput',false))
% pick the z levels you want to plot
% note that these are WRT the non-offset object
contour(X2,Y2,Z2,[0 1 2 3 4])
would give this:

I'm sure there are more elegant ways of dealing with an offset contour.
类别
在 帮助中心 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!