Scientific Visualisations with Python

Creating a 3D Point Cloud Visualization with PyVista

PyVista allows us to create interactive 3D visualizations of spatial data. In this example, we display temperature values at different spatial coordinates and use clipping to show only part of the point cloud.

Preparing the Data

First, we load the dataset and select observations corresponding to a specific time:

df = pd.read_csv("../data/spatial_grid.csv")
t0 = 8
df = df[df["time"] == t0].copy()
points = df[["x", "y", "z"]].to_numpy()

The points array contains the 3D coordinates of each observation.

Creating the Point Cloud

We use PolyData() to create a 3D point cloud and attach temperature values to it:

cloud = pv.PolyData(points)
cloud["temperature"] = df["temperature"].to_numpy()

The temperature values will be used to determine the colors of the points.

Clipping the Point Cloud

The clip() method allows us to remove part of the dataset using a plane:

clipped = cloud.clip(
    normal="y",
    origin=(0, 0, 0),
    invert=False
)

The normal parameter defines the clipping plane direction, while origin specifies its position. The invert parameter controls which side of the plane is retained.

Creating the 3D Visualization

Finally, we create a plotter and display the clipped point cloud:

plotter = pv.Plotter()
plotter.add_mesh(
    clipped,
    scalars="temperature",
    cmap="jet",
    point_size=14,
    render_points_as_spheres=True,
    show_scalar_bar=True
)
plotter.add_axes()
plotter.show_grid()
plotter.show()

The main parameters are:
• scalars – determines which data values control point colors.
• cmap – defines the color map.
• point_size – controls the size of the points.
• render_points_as_spheres – displays points as 3D spheres.
• show_scalar_bar – displays the color legend.

The add_axes() and show_grid() methods add coordinate axes and a reference grid.

Summary

In this example, we used:
• PolyData() – creates a 3D point cloud.
• clip() – removes part of the point cloud using a plane.
• Plotter() – creates the visualization window.
• add_mesh() – displays the 3D data.
• add_axes() and show_grid() – add spatial reference elements.

Creating a 3D Volume Visualization with PyVista

PyVista allows us to create interactive 3D volume visualizations. In this example, we visualize temperature distribution in a 3D spatial grid at a selected time.

Preparing the Data

First, we load the dataset and select observations corresponding to a specific time:

df = pd.read_csv("../data/spatial_grid.csv")
t0 = 8
df = df[df["time"] == t0].copy()

Creating the 3D Grid

We use ImageData() to create a regular 3D grid:

n = 35
grid = pv.ImageData(
    dimensions=(n, n, n),
    spacing=(10 / (n - 1), 10 / (n - 1), 10 / (n - 1)),
    origin=(-5, -5, -5)
)

The parameters define the grid dimensions, spacing between points, and its origin.

Next, we assign temperature values to the grid:

grid.point_data["temperature"] = (
    df["temperature"].to_numpy()
    .reshape((n, n, n))
    .ravel(order="F")
)

The reshape() method converts the temperature values into a 3D array, while ravel() flattens it into the format required by PyVista.

Creating the Volume Visualization

We create a plotter and display the temperature distribution using add_volume() :

plotter = pv.Plotter()
plotter.add_volume(
    grid,
    scalars="temperature",
    cmap="turbo",
    opacity="sigmoid",
    shade=True,
    ambient=0.3,
    diffuse=0.8,
    specular=0.2
)

The main parameters are:
• scalars – specifies the data used for coloring the volume.
• cmap – defines the color map.
• opacity – controls volume transparency.
• shade – enables shading.
• ambient, diffuse, and specular – control lighting effects.

The optional clim parameter can be used to specify the minimum and maximum values of the color scale.

Displaying the Visualization

Finally, we add coordinate axes and a reference grid:

plotter.add_axes()
plotter.show_grid()
plotter.show()

The show() method opens an interactive 3D visualization that allows us to explore the temperature distribution from different angles.

Creating 3D Isosurfaces with PyVista

PyVista allows us to visualize 3D scalar fields using isosurfaces. In this example, we display surfaces representing selected temperature values within a 3D spatial grid.

Preparing the Data

First, we load the dataset and select observations corresponding to a specific time:

df = pd.read_csv("../data/spatial_grid.csv")
t0 = 2
df = df[df["time"] == t0].copy()

Creating the 3D Grid

We create a regular 3D grid using ImageData():

n = 35
grid = pv.ImageData(
    dimensions=(n, n, n),
    spacing=(10 / (n - 1), 10 / (n - 1), 10 / (n - 1)),
    origin=(-5, -5, -5)
)

Next, we assign temperature values to the grid:

grid.point_data["temperature"] = (
    df["temperature"].to_numpy()
    .reshape((n, n, n))
    .ravel(order="F")
)

Extracting Isosurfaces

We use contour() to generate surfaces corresponding to selected temperature values:

surfaces = grid.contour(
    isosurfaces=[30, 50, 80],
    scalars="temperature"
)

The isosurfaces parameter specifies the temperature levels for which surfaces are generated. Each surface represents points with approximately the same temperature value.

Creating the 3D Visualization

We use add_mesh() to display the extracted surfaces:

plotter = pv.Plotter()
plotter.add_mesh(
    surfaces,
    scalars="temperature",
    preference="point",
    cmap="turbo",
    opacity=0.5,
    smooth_shading=True,
    ambient=0.35,
    diffuse=0.8,
    specular=0.1,
    show_scalar_bar=True
)

The main parameters are:
• scalars – specifies the values used for coloring.
• preference – selects point data for coloring.
• cmap – defines the color map.
• opacity – controls surface transparency.
• smooth_shading – enables smoother surface rendering.
• ambient, diffuse, and specular – control lighting effects.
• show_scalar_bar – displays the color legend.

Displaying the Visualization

Finally, we add coordinate axes and a reference grid:

plotter.add_axes()
plotter.show_grid()
plotter.show()

Visualizing a 3D Velocity Field with PyVista

PyVista allows us to visualize vector fields using 3D arrows (glyphs). In this example, we display velocity vectors at different spatial locations, with arrow direction and size representing the velocity field.

Preparing the Data

First, we load the dataset and select observations corresponding to a specific time:

df = pd.read_csv("../data/spatial_grid.csv")
t0 = 2
df = df[df["time"] == t0]

Next, we extract spatial coordinates and velocity components:

points = df[["x", "y", "z"]].to_numpy()
velocity = df[
    ["velocity_x", "velocity_y", "velocity_z"]
].to_numpy()
speed = np.linalg.norm(velocity, axis=1)

The points array contains 3D coordinates, while velocity stores velocity vectors. The np.linalg.norm() function calculates the magnitude (speed) of each vector.

Creating the Vector Field

We create a point cloud and attach velocity and speed data:

cloud = pv.PolyData(points)
cloud["velocity"] = velocity
cloud["speed"] = speed

Generating 3D Arrows

We use glyph() to represent velocity vectors as arrows:

arrows = cloud.glyph(
    orient="velocity",
    scale="speed",
    factor=0.4
)

The main parameters are:
• orient – determines arrow orientation using velocity vectors.
• scale – controls arrow size based on speed.
• factor – adjusts the overall arrow size.

Creating the Visualization

We display the generated arrows using add_mesh() :

plotter = pv.Plotter()
plotter.add_mesh(
    arrows,
    scalars="speed",
    preference="point",
    cmap="turbo",
    clim=[speed.min(), speed.max()],
    smooth_shading=True,
    ambient=0.4,
    diffuse=0.8,
    specular=0.2,
    show_scalar_bar=True
)

The arrow colors represent velocity magnitude. The cmap parameter defines the color map, while clim sets its minimum and maximum values. The lighting parameters control the visual appearance of the arrows.

Displaying the Visualization

Finally, we add coordinate axes and a reference grid:

plotter.add_axes()
plotter.show_grid()
plotter.show()