2012-09-09 86 views
5

我的意思是圓錐體或圓盤正在以其對稱軸移動或旋轉。準確地說,我創建了這個隨時間不斷變化的軸:用matplotlib創建3D錐體或圓盤並不斷更新其對稱軸

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

我需要總是垂直於該軸的圓錐或圓的面。我第一次嘗試一個更簡單的創建一個2D的圓然後將其提起到我想要的位置:

circle = Circle((0, 0), .3, color='r') 
ax.add_patch(circle) 
art3d.pathpatch_2d_to_3d(circle, z=1) 

但不會使圓的臉垂直於移動軸。我不知道matplotlib中有什麼函數可以用來旋轉圓錐/圓的那個面嗎?

如果我從另一種方式開始創建一個3D物體,就像一個橢圓體一樣,問題仍然存在:我如何讓物體像對稱軸一樣像剛體一樣運動(堅持其軸),而不是掛在那裏的燈籠(僅限固定點)?

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") 
+0

euler_rot youy可以提供一個工作的例子嗎? –

回答

2
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沿着旋轉後的z軸的方向指向的行。 (在這種情況下,psi將不起作用,因爲錐體圍繞z軸是軸對稱的)。另請參閱rotation matrix

要繪製一個單圈由h0沿着正常的轉移:

x=r0*np.cos(u) 
y=r0*np.sin(u) 
z=h0*np.ones(len(x)) 
XYZ = np.vstack([x,y,z])  
xp,yp,zp = euler_rot(XYZ,phi,theta,psi) 
ax.plot(xp,yp,zs=zp) 

它作爲一個練習從給定的矢量得到歐拉角。

gist