如何使用PyVista将矢量箭头添加到3D绘图(如ABC字段)?

2024-05-23 15:14:20 发布

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

当我添加像Plotting Glyphs (Vectors)这样的向量箭头时,我已经绘制了ABC feild等高线。我不知道如何为3D数据制作网格

import pyvista as pv
import numpy as np
from numpy import mgrid

xmin = -800.
xmax = 800.
Lx = xmax-xmin

B0 = 1
k = 1
alpha = 2.0*np.pi*k/Lx

x, y, z = Lx*mgrid[0:1:51j, 0:1:51j, 0:1:51j]

Bx = B0*(np.sin(alpha*z) + np.cos(alpha*y))
By = B0*(np.sin(alpha*x) + np.cos(alpha*z))
Bz = B0*(np.sin(alpha*y) + np.cos(alpha*x))

B = Bx+By+Bz

grid = pv.StructuredGrid(x, y, z)
grid["ABC field 3D isocontour"] = B.flatten()
contours = grid.contour()

pv.set_plot_theme('document')
p = pv.Plotter()
p.add_mesh(contours)

#contours.plot(show_edges=True, show_grid=True, cpos="xy")
contours.plot(show_grid=True,screenshot='abc3d.png')
#p.show(screenshot='abc3d.png')

结果:

ABC field contour plot


Tags: importalphatrueplotasshownpsin
1条回答
网友
1楼 · 发布于 2024-05-23 15:14:20

多亏了开发人员的响应,我现在更好地理解了mesh的定义。上述数据集需要np.column_stack()

import pyvista as pv
import numpy as np
from numpy import mgrid
import matplotlib.pyplot as plt

print('initializing domain')
xmin = -800.
xmax = 800.
Lx = xmax-xmin
B0 = 1
k = 1
alpha = 2.0*np.pi*k/Lx
x, y, z = Lx*mgrid[0:1:51j, 0:1:51j, 0:1:51j]


print('initializing 3D B field')
Bx = B0*(np.sin(alpha*z) + np.cos(alpha*y))
By = B0*(np.sin(alpha*x) + np.cos(alpha*z))
Bz = B0*(np.sin(alpha*y) + np.cos(alpha*x))
B = np.column_stack((Bx.ravel(), By.ravel(), Bz.ravel()))
grid = pv.StructuredGrid(x, y, z)
grid["ABC field magnitude"] = np.linalg.norm(B, axis=1)
grid["ABC field vectors"] = B
grid.set_active_vectors("ABC field vectors")
contours = grid.contour(8, scalars="ABC field magnitude")
arrows = contours.glyph(orient="ABC field vectors", factor=200.0)
arrows_grid = grid.glyph(orient="ABC field vectors", factor=50.0)


print('plotting')
pv.set_plot_theme('document')
p = pv.Plotter(notebook=0, shape=(2,2))
cmap = plt.cm.get_cmap("viridis", 4)
#p.background_color='white'
#p.window_size
p.add_mesh(grid, cmap=cmap)
p.add_mesh(arrows_grid)
p.subplot(0,1)
slices = grid.slice_orthogonal(x=20, y=20, z=30)
#slices = grid.slice_orthogonal()
p.add_mesh(slices, cmap=cmap)
p.subplot(1,0)
p.add_mesh(contours, opacity=1)
p.show_grid()
p.subplot(1,1)
p.add_mesh(arrows)
p.show_grid()

p.link_views()
p.view_isometric()
p.show(screenshot='abc3d.png')

结果

Arnold–Beltrami–Childress Mangetic Field

相关问题 更多 >