创建三维圆锥体或磁盘,并使用matplotlib不断更新其对称轴

2024-06-12 04:19:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的意思是圆锥体或圆盘是沿着它的对称轴移动或旋转的。确切地说,我正在创造这个轴,它随着时间不断变化:

line = ax.plot([x,0],[y,0],[z,z- n_o],color='#000066', marker= 'o')

我需要圆锥体或圆的面始终垂直于那个轴。我尝试了一个简单的方法,先创建一个二维圆,然后将其提升到我想要的位置:

^{pr2}$

但这不会使圆的表面垂直于运动轴。我想知道matplotlib中有没有函数可以用来旋转圆锥体/圆的那个面?在

如果,我从另一个角度开始创建一个三维对象,比如椭球体,那么问题仍然存在:我如何让对象像刚体一样沿着其对称轴移动,而不是像灯笼一样悬挂在那里(只附着在固定点上)?在

u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x=np.cos(u)*np.sin(v)
y=np.sin(u)*np.sin(v)
z=.3*np.cos(v)
ax.plot_wireframe(x, y, z, color="r")

Tags: 对象方法plotnpline时间pisin
1条回答
网友
1楼 · 发布于 2024-06-12 04:19:43
from mpl_toolkits.mplot3d import Axes3D



def euler_rot(XYZ,phi,theta,psi):
    '''Returns the points XYZ rotated by the given euler angles'''


    ERot = np.array([[np.cos(theta)*np.cos(psi), 
                      -np.cos(phi)*np.sin(psi) + np.sin(phi)*np.sin(theta)*np.cos(psi), 
                      np.sin(phi)*np.sin(psi) + np.cos(phi)*np.sin(theta)*np.cos(psi)],
                     [np.cos(theta)*np.sin(psi), 
                      np.cos(phi)*np.cos(psi) + np.sin(phi)*np.sin(theta)*np.sin(psi),
                      -np.sin(phi)*np.cos(psi) + np.cos(phi)*np.sin(theta)*np.sin(psi)],
                     [-np.sin(theta),
                      np.sin(phi)*np.cos(theta),
                      np.cos(phi)*np.cos(theta)]])

    return ERot.dot(XYZ)


u = np.linspace(0,2*np.pi,50)
num_levels = 10

r0 = 1 # maximum radius of cone
h0 = 5 # height of cone

phi = .5 # aka alpha
theta = .25 # aka beta
psi = 0 # aka gamma



norm = np.array([0,0,h0]).reshape(3,1)
normp = euler_rot(norm,phi,theta,psi)


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot([0,normp[0]],[0,normp[1]],zs= [0,normp[2]])

x = np.hstack([r0*(1-h)*np.cos(u) for h in linspace(0,1,num_levels)])
y = np.hstack([r0*(1-h)*np.sin(u) for h in linspace(0,1,num_levels)])
z = np.hstack([np.ones(len(u))*h*h0 for h in linspace(0,1,num_levels)])
XYZ = np.vstack([x,y,z])

xp,yp,zp = euler_rot(XYZ,phi,theta,psi) 
ax.plot_wireframe(xp,yp,zp)

这将在通过Euler anglesphithetapsi旋转后,围绕该直线绘制一个圆锥体。(在这种情况下,psi将不起作用,因为圆锥体围绕z轴是轴对称的)另请参见rotation matrix。在

要绘制沿法线移动h0的单个圆:

^{pr2}$

这是一个练习,从给定的向量得到欧拉角。在

euler_rotgist

相关问题 更多 >