用颤振器和Meshgrid绘制离散的3D矢量场

时间:2019-06-25 17:26:05

标签: python numpy matplotlib

我试图绘制一个矢量场,该矢量场采用用户输入的导线几何形状,并在我使用meshgrid创建的3D网格上输出3D场。

# get_field returns a single list [i,j,k] of the field vector evaluated at the input point [x,y,z]

# Create grid to evaluate field at points 
xx, yy, zz = np.meshgrid(np.arange(minX, maxX, spacing),
                     np.arange(minY, maxY, spacing),
                     np.arange(minZ, maxZ, spacing))

# Evaluate field at points
field = get_field([xx,yy,zz])

# Plot
ax.quiver(xx, yy, zz, field, length=1)
plt.show()

我在将每个方向向量[i,j,k]映射到一个numpy数组或列表进行绘图时遇到麻烦

编辑 我放弃了蛮力强迫。

xx = np.arange(self.minX, self.maxX, spacing)
yy = np.arange(self.minY, self.maxY, spacing)
zz = np.arange(self.minZ, self.maxZ, spacing)

u = []
v = []
w = []

for x in xx:
     for y in yy:
          for z in zz:
                 # get_field returns a single list [i,j,k] of the field vector evaluated at the input point [x,y,z]
                 field = get_field([x, y, z])
                 u.append(field[0])
                 v.append(field[1])
                 w.append(field[2])
u = np.asarray(u)
v = np.asarray(v)
w = np.asarray(w)
plt_ax.quiver(xx, yy, zz, u, v, w, length=1, normalize=True)

我以为这种实现至少可以(非常糟糕),但是现在我收到一个错误,即使有足够的点可以匹配xx,yy,zz的尺寸。

ValueError: shape mismatch: objects cannot be broadcast to a single shape

0 个答案:

没有答案