【问题标题】:How do I plot a vector field within an arbitrary plane using Python?如何使用 Python 在任意平面内绘制矢量场?
【发布时间】:2018-01-25 19:21:44
【问题描述】:

我在一个形状为 numpy 的数组(zlength、ylength、xlength、3)中有一个 3d 速度矢量场。 “3”包含速度分量 (u,v,w)。

我可以很容易地使用 quiver 在正交 x-y、x-z 和 y-z 平面上绘制矢量场,例如

X, Y = np.meshgrid(xvalues, yvalues)

xyfieldfig = plt.figure()
xyfieldax = xyfieldfig.add_subplot(111)
Q1 = xyfieldax.quiver(X, Y, velocity_field[zslice,:,:,0], velocity_field[zslice,:,:,1])

但是,我希望能够查看任意平面内的速度场。

我尝试通过以下方式将速度场投影到平面上:

projected_field = np.zeros(zlength,ylength,xlength,3)
normal = (nx,ny,nz) #normalised normal to the plane

for i in range(zlength):
    for j in range(ylength):
        for k in range(xlength):
            projected_field[i,j,m] = velocity_field[i,j,m] - np.dot(velocity_field[i,j,m], normal)*normal

但是,这(当然)仍然给我留下了一个具有相同形状的 3d numpy 数组:(zlength,ylength,xlength,3)。现在,projected_field 包含位于每个局部 (x,y,z) 位置的平面内的每个 (x,y,z) 位置的速度矢量。

如何将velocity_field 投影到单个平面上?或者,我现在如何在一个平面上绘制我的 projection_field?

提前致谢!

【问题讨论】:

标签: python numpy matplotlib vector 3d


【解决方案1】:

你已经接近了。 Daniel F 的建议是对的,您只需要知道如何进行插值。这是一个有效的例子

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import scipy.interpolate  


def norm(v,axis=0):
    return np.sqrt(np.sum(v**2,axis=axis))

#Original velocity field
xpoints = np.arange(-.2, .21, 0.05)
ypoints = np.arange(-.2, .21, 0.05)
zpoints = np.arange(-.2, .21, 0.05)

x, y, z = np.meshgrid(xpoints,ypoints,zpoints,indexing='ij')

#Simple example
#(u,v,w) are the components of your velocity field
u = x
v = y
w = z 

#Setup a template for the projection plane. z-axis will be rotated to point
#along the plane normal
planex, planey, planez  =
    np.meshgrid(np.arange(-.2,.2001,.1), 
                 np.arange(-.2,.2001,.1), [0.1], 
                 indexing='ij')

planeNormal = np.array([0.1,0.4,.4])
planeNormal /= norm(planeNormal)

#pick an arbirtrary vector for projection x-axis
u0 = np.array([-(planeNormal[2] + planeNormal[1])/planeNormal[0], 1, 1])
u1 = -np.cross(planeNormal,u0) 
u0 /= norm(u0)
u1 /= norm(u1)

#rotation matrix
rotation = np.array([u0,u1,planeNormal]).T


#Rotate plane to get projection vertices
rotatedVertices = rotation.dot( np.array( [planex.flatten(), planey.flatten(), planez.flatten()]) ).T


#Now you can interpolate gridded vector field to rotated vertices
uprime = scipy.interpolate.interpn( (xpoints,ypoints,zpoints), u, rotatedVertices, bounds_error=False )
vprime = scipy.interpolate.interpn( (xpoints,ypoints,zpoints), v, rotatedVertices, bounds_error=False )
wprime = scipy.interpolate.interpn( (xpoints,ypoints,zpoints), w, rotatedVertices, bounds_error=False )

#Projections
cosineMagnitudes = planeNormal.dot( np.array([uprime,vprime,wprime]) )

uProjected = uprime - planeNormal[0]*cosineMagnitudes
vProjected = vprime - planeNormal[1]*cosineMagnitudes
wProjected = wprime - planeNormal[2]*cosineMagnitudes

如果您想花哨的话,可以使用一些tensordot 操作来减少行数。在meshgrid 中没有indexing='ij' 也可以使用这个或一些接近的变体。

原始字段:

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.quiver(x, y, z, u, v, w, length=0.1, normalize=True)

投影场:

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.quiver(rotatedVertices[:,0], rotatedVertices[:,1], rotatedVertices[:,2], 
          uprime, vprime,wprime, length=0.5, color='blue', label='Interpolation only')
ax.quiver(rotatedVertices[:,0], rotatedVertices[:,1], rotatedVertices[:,2], 
          uProjected, vProjected, wProjected, length=0.5, color='red', label='Interpolation + Projection')


plt.legend()

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 2021-11-04
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多