早上好,我想使用 pvpython 在 VTK 文件中制作一个 3D 体积的切片,然后我想保存此图像(可能使用以白色为中心的对称调色板)。
具体来说,我想用 python 制作以下用 Paraview 拍摄的图像:
我是 pvpython 或 python 的新手,所以我报告了我的第一个脚本,但它不起作用:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.interpolate import griddata
from paraview import simple as pvs
import numpy as np
import vtk
import matplotlib
from vtk.util.numpy_support import vtk_to_numpy
reader = vtk.vtkDataSetReader()
reader.SetFileName("../deltau-deltau.vtk")
reader.ReadAllScalarsOn()
reader.Update()
plane = vtk.vtkPlane()
plane.SetOrigin(16, 0, 0)
plane.SetNormal(1, 0, 0)
cutter = vtk.vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInputConnection(reader.GetOutputPort())
cutter.Update()
data = cutter.GetOutput()
# Coordinates of nodes in the mesh
nodes_vtk_array= reader.GetOutput().GetPoints().GetData()
#Get the coordinates of the nodes and their variables
nodes_nummpy_array = vtk_to_numpy(nodes_vtk_array)
x=nodes_nummpy_array[:,0]
y=nodes_nummpy_array[:,1]
z=nodes_nummpy_array[:,2]
variable_numpy_array = vtk_to_numpy(data)
var = variable_numpy_array
# Figure bounds
npts = 100
xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
zmin, zmax = min(z), max(z)
# Grid
xi = np.linspace(xmin, xmax, npts)
yi = np.linspace(ymin, ymax, npts)
zi = np.linspace(zmin, zmax, npts)
# The mesh is unstructured, so
var_plot = griddata((z, y), var, (zi[None,:], yi[:,None]), method='cubic')
plt.plot(zi, yi, var_plot)
#plt.colorbar()
plt.savefig("figure.png")