
VTK (Visualization Toolkit) allows us to create advanced 3D scientific visualizations. In this example, we visualize a velocity field using streamlines, which show the flow direction and structure of the vector field.
First, we load the dataset and select observations corresponding to a specific time:
df = pd.read_csv("../data/spatial_grid.csv")
df = df[df["time"] == 5]
n = 35
Next, we define a helper function to reshape the data into a 3D grid:
def get_field(name):
return df[name].to_numpy().reshape((n, n, n)).ravel(order="F")
We extract the velocity components and speed:
velocity = np.column_stack([
get_field("velocity_x"),
get_field("velocity_y"),
get_field("velocity_z")
])
speed = get_field("speed")
We create a 3D image grid using vtkImageData():
grid = vtk.vtkImageData()
grid.SetDimensions(n, n, n)
grid.SetOrigin(-5, -5, -5)
grid.SetSpacing(10 / 34, 10 / 34, 10 / 34)
The grid defines the spatial dimensions, origin, and spacing between points.
We convert NumPy arrays into VTK arrays and attach them to the grid:
vectors = numpy_to_vtk(velocity, deep=True)
vectors.SetNumberOfComponents(3)
vectors.SetName("velocity")
scalars = numpy_to_vtk(speed, deep=True)
scalars.SetName("speed")
grid.GetPointData().SetVectors(vectors)
grid.GetPointData().SetScalars(scalars)
The velocity vectors define the flow direction, while speed will be used to color the streamlines.
We define starting points for the streamlines using vtkPointSource():
seeds = vtk.vtkPointSource()
seeds.SetCenter(0, 0, 0)
seeds.SetNumberOfPoints(30)
seeds.SetRadius(2)
Next, we use vtkStreamTracer() to calculate the streamlines:
stream = vtk.vtkStreamTracer()
stream.SetInputData(grid)
stream.SetSourceConnection(seeds.GetOutputPort())
stream.SetIntegratorTypeToRungeKutta4()
stream.SetIntegrationDirectionToBoth()
stream.SetMaximumPropagation(30)
stream.SetInitialIntegrationStep(0.1)
stream.SetMaximumIntegrationStep(0.2)
stream.SetMinimumIntegrationStep(0.01)
stream.SetComputeVorticity(True)
The main parameters are:
• SetSourceConnection() – defines the starting points.
• SetIntegratorTypeToRungeKutta4() – selects the Runge-Kutta 4 integration method.
• SetIntegrationDirectionToBoth() – traces the flow in both directions.
• SetMaximumPropagation() – limits streamline length.
• SetComputeVorticity() – enables vorticity calculation.
To make the streamlines more visible in 3D, we use vtkTubeFilter():
tube = vtk.vtkTubeFilter()
tube.SetInputConnection(stream.GetOutputPort())
tube.SetRadius(0.025)
tube.SetNumberOfSides(8)
tube.CappingOn()
This converts thin streamlines into tubes with a specified radius and number of sides.
We use vtkPolyDataMapper() to map the geometry and color it according to speed:
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(tube.GetOutputPort())
mapper.SetScalarModeToUsePointFieldData()
mapper.SelectColorArray("speed")
mapper.SetScalarRange(speed.min(), speed.max())
Next, we create an actor and renderer:
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderer.SetBackground(1, 1, 1)
renderer.AddActor(actor)
The actor represents the 3D geometry, while the renderer manages the scene and background.
Finally, we create the render window and initialize user interaction:
window = vtk.vtkRenderWindow()
window.AddRenderer(renderer)
window.SetSize(1100, 850)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(window)
interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
renderer.ResetCamera()
window.Render()
interactor.Initialize()
interactor.Start()
The vtkRenderWindow() displays the visualization, while vtkRenderWindowInteractor() enables interactive camera control, including rotation, zooming, and panning.
In this example, we used:
• vtkImageData() – creates a regular 3D grid.
• numpy_to_vtk() – converts NumPy arrays into VTK data arrays.
• vtkStreamTracer() – generates streamlines from a velocity field.
• vtkTubeFilter() – converts streamlines into 3D tubes.
• vtkPolyDataMapper() – maps geometry and scalar values.
• vtkRenderer() and vtkRenderWindow() – display the 3D scene.

VTK allows us to interactively explore 3D datasets by clipping them with a movable plane. In this example, we visualize temperature distribution and use an interactive plane widget to control which part of the volume is displayed.
First, we load the dataset and select observations corresponding to a specific time:
df = pd.read_csv("../data/spatial_grid.csv")
df = df[df["time"] == 5]
n = 35
temperature = df["temperature"].to_numpy().reshape((n, n, n)).ravel(order="F")
We create a regular 3D grid using vtkImageData():
grid = vtk.vtkImageData()
grid.SetDimensions(n, n, n)
grid.SetOrigin(-5, -5, -5)
grid.SetSpacing(10 / 34, 10 / 34, 10 / 34)
We convert the temperature array into a VTK data array and attach it to the grid:
values = numpy_to_vtk(temperature, deep=True)
values.SetName("temperature")
grid.GetPointData().SetScalars(values)
We use vtkPlane() to define a plane that divides the 3D dataset:
plane = vtk.vtkPlane()
plane.SetOrigin(0, 0, 0)
plane.SetNormal(1, 0, 0)
The SetOrigin() method defines the plane position, while SetNormal() specifies its orientation.
Next, we create a clipping filter:
clip = vtk.vtkTableBasedClipDataSet()
clip.SetInputData(grid)
clip.SetClipFunction(plane)
clip.InsideOutOn()
The clipping filter removes part of the dataset according to the plane position and orientation.
We use vtkDataSetMapper() and vtkActor() to display the clipped data:
mapper = vtk.vtkDataSetMapper()
mapper.SetInputConnection(clip.GetOutputPort())
mapper.SetScalarRange(temperature.min(), temperature.max())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderer.SetBackground(1, 1, 1)
renderer.AddActor(actor)
To allow users to move and rotate the clipping plane, we create an interactive widget:
widget = vtk.vtkImplicitPlaneWidget2()
widget.SetInteractor(interactor)
representation = vtk.vtkImplicitPlaneRepresentation()
representation.SetPlaceFactor(1.0)
representation.PlaceWidget(grid.GetBounds())
representation.SetOrigin(0, 0, 0)
representation.SetNormal(1, 0, 0)
widget.SetRepresentation(representation)
The vtkImplicitPlaneWidget2() object provides interactive plane manipulation,
while vtkImplicitPlaneRepresentation() defines its position and orientation.
We define a callback that updates the clipping whenever the plane is moved:
def update_clip(widget, event):
representation.GetPlane(plane)
clip.Update()
window.Render()
widget.AddObserver(
vtk.vtkCommand.InteractionEvent,
update_clip
)
The GetPlane() method retrieves the current plane parameters. The clipping filter is then updated and the visualization is rendered again.
Finally, we create the rendering window and enable interaction:
window = vtk.vtkRenderWindow()
window.AddRenderer(renderer)
window.SetSize(1100, 850)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(window)
interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
renderer.ResetCamera()
widget.On()
window.Render()
interactor.Initialize()
interactor.Start()
Users can now move and rotate the clipping plane directly in the 3D window to explore different parts of the temperature distribution.