Visualisation of the 3d scalar field
import numpy as np
import pyvista as pv
#
# ---------------------------------------------------------
# Load scalar field: expected columns = x y z value
# ---------------------------------------------------------
data = np.loadtxt("./scalar_field.txt")
#
x, y, z, s = data.T
#
# Create point cloud
points = np.vstack((x, y, z)).T
cloud = pv.PolyData(points)
# Add scalar array
cloud["scalar"] = s
#
# Create sphere glyphs
#
sphere = pv.Sphere(radius=0.02) # adjust radius
#
glyphs = cloud.glyph(
geom=sphere,
scale=False, # True or False
orient=False,
)
#
# Plot
#
plotter = pv.Plotter()
#
plotter.add_mesh(
glyphs,
scalars="scalar",
cmap="viridis",
show_scalar_bar=True,
scalar_bar_args={"title": "Scalar Value"},
)
#
plotter.set_background("white")
plotter.add_axes()
plotter.show()
Visualisation of the 3d vector field
import numpy as np
import pyvista as pv
#
data = np.loadtxt("./vector_field.txt")
#
x, y, z, vx, vy, vz = data.T
#
# Compute magnitude for coloring
magnitude = np.linalg.norm(np.vstack((vx, vy, vz)), axis=0)
#
# Create PyVista point cloud
points = np.vstack((x, y, z)).T
cloud = pv.PolyData(points)
#
# Add vector & magnitude arrays
cloud["vectors"] = np.vstack((vx, vy, vz)).T
cloud["magnitude"] = magnitude
#
# Create arrow glyphs
# scale="magnitude" → scale arrows by vector magnitude
# orient="vectors" → orient arrows by direction
glyphs = cloud.glyph(
orient="vectors",
scale="magnitude",
factor=0.1, # global scaling factor for arrow size
geom=pv.Arrow(), # arrow shape
)
#
# Plot
plotter = pv.Plotter()
#
plotter.add_mesh(
glyphs,
scalars="magnitude",
cmap="viridis",
show_scalar_bar=True,
scalar_bar_args={"title": "Vector Magnitude"},
)
#
plotter.set_background("white")
plotter.add_axes()
plotter.show()