Scientific Visualisations with Python

Create Sample Data

Before visualizing fields in 3D, we first generate synthetic data to work with. This allows us to test visualization tools without requiring real experimental or simulation data.

Scalar Field Data

A scalar field assigns a single value to each point in space (e.g., temperature, pressure, or some potential).

import numpy as np
a = np.linspace(0,1)
x, y = np.meshgrid(a,a)
x = x.flatten()
y = y.flatten()
z = np.sin(x+y) + np.cos(3*y)
v = z + np.random.rand(z.size)*0.1
data = np.vstack([x,y,z,v]).T
np.savetxt("./scalar_field.txt", data, fmt="%-10.4f")

• Columns in scalar_field.txt: $x, y, z, v$

• Each row represents a point in space with its scalar value $v$

Vector Field Data

A vector field assigns a vector to each point in space (e.g., velocity, force, or displacement). Each vector has components (vx, vy, vz)

import numpy as np
a = np.linspace(0,1, 20)
x, y = np.meshgrid(a,a)
x = x.flatten()
y = y.flatten()
z = np.sin(x+y) + np.cos(3*y)
vx = np.sin(5*x)
vy = y
vz = z
data = np.vstack([x,y,z,vx,vy,vz]).T
np.savetxt("./vector_field.txt", data, fmt="%-10.4f")

• Columns in vector_field.txt: $x, y, z, vx, vy, vz$
• Each row represents a point with a vector attached to it.