Python EVTK Library
pyevtk is a Python library for exporting numerical data (like structured grids, unstructured grids, and points) into binary VTK (Visualization Toolkit) files, enabling visualization in tools like ParaView or VisIt. As of version 1.6.0, it supports various data types and has an active, albeit slow, release cadence.
Common errors
-
ModuleNotFoundError: No module named 'evtk'
cause Attempting to import from the deprecated `evtk` package, which might not be installed or available as an alias in the current environment/version.fixChange all imports from `evtk` to `pyevtk`, e.g., `from pyevtk.hl import gridToVTK`. -
TypeError: Expected a numpy array for 'data' but got <class 'list'>
cause Input data (e.g., field values, coordinate arrays) was provided as a Python list or non-NumPy array.fixConvert the input data to a NumPy array before passing it to `pyevtk` functions, e.g., `import numpy as np; data = np.asarray(my_list)`. -
ValueError: Data dimensions do not match grid dimensions.
cause The shape of the data array (e.g., `cellData` or `pointData`) does not correspond to the dimensions of the grid (e.g., `(nx, ny, nz)` for structured grids).fixCarefully check the expected dimensions for the specific VTK export function being used. For cell data on an `(nx, ny, nz)` grid, the data array should typically have shape `(nx-1, ny-1, nz-1)`. For point data, it should be `(nx, ny, nz)`.
Warnings
- deprecated Using `from evtk.hl import ...` is deprecated. The `evtk` package is an alias for `pyevtk` for backward compatibility but will issue a `DeprecationWarning` and may be removed in future versions.
- gotcha All input data arrays (e.g., `data`, `x`, `y`, `z` coordinates) must be NumPy arrays. Passing standard Python lists or other array-like objects will result in `TypeError` or unexpected behavior.
- gotcha VTK files generated by `pyevtk` often assume cell-centered data for structured grids. Misinterpreting this can lead to data being shifted by half a cell when visualized in tools like ParaView if your data is node-centered.
Install
-
pip install pyevtk
Imports
- gridToVTK
from evtk.hl import gridToVTK
from pyevtk.hl import gridToVTK
- pointsToVTK
from pyevtk.hl import pointsToVTK
- imageToVTK
from pyevtk.hl import imageToVTK
Quickstart
import numpy as np
from pyevtk.hl import gridToVTK
# Define grid dimensions
nx, ny, nz = 64, 64, 64
# Coordinates
x = np.arange(0, nx, 1, dtype='float64')
y = np.arange(0, ny, 1, dtype='float64')
z = np.arange(0, nz, 1, dtype='float64')
# Cell data (e.g., temperature)
temp = np.random.rand(nx - 1, ny - 1, nz - 1)
# Point data (e.g., pressure)
press = np.random.rand(nx, ny, nz)
# Export to VTK file
gridToVTK(
"output_grid",
x, y, z,
cellData={'temperature': temp},
pointData={'pressure': press}
)
print("VTK file 'output_grid.vts' generated successfully.")