Yi-*_*ang 5 python matplotlib python-2.7
我的意思是圆锥或圆盘正在以其对称轴移动或旋转.确切地说,我正在创建这个轴,它随时间不断变化:
line = ax.plot([x,0],[y,0],[z,z- n_o],color='#000066', marker= 'o')
Run Code Online (Sandbox Code Playgroud)
我需要锥体或圆的面始终垂直于该轴.我首先尝试更简单,创建一个2D圆圈,然后将其提升到我想要的位置:
circle = Circle((0, 0), .3, color='r')
ax.add_patch(circle)
art3d.pathpatch_2d_to_3d(circle, z=1)
Run Code Online (Sandbox Code Playgroud)
但这不会使圆的面垂直于移动轴.我想知道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")
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
这将在旋转欧拉角 phi, ,后围绕指向 z 轴方向的线绘制一个圆锥体theta。psi(在这种情况下psi不会产生任何影响,因为圆锥体绕 z 轴轴对称)另请参阅旋转矩阵。
要绘制沿法线移动的单个圆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)
Run Code Online (Sandbox Code Playgroud)
留下的练习是从给定向量获取欧拉角。
euler_rot要点
| 归档时间: |
|
| 查看次数: |
1891 次 |
| 最近记录: |