import pyvista as pv
# Create a simple mesh (sphere)
= pv.Sphere()
sphere
# Plot the mesh
sphere.plot()
Simple Visualisations and Examples
After the installation, we can start using PyVista for 3D visualization. Here area some simple examples to plot and analyse 3D mesh:
We will start with a simple 3 dimensional sphere.
Use a topographic surface to create a 3D terrain-following mesh. In this example, we demonstrate a simple way to make a 3D grid/mesh that follows a given topographic surface.
from pyvista import examples
= examples.load_random_hills() # automatically download
mesh = mesh.contour()
contours contours
Header | Data Arrays | ||||||||||||||||||||||||||||||||||
|
|
mesh.plot()
If you have a structured dataset like a pyvista.ImageData or pyvista.RectilinearGrid, you can clip it using the pyvista.Plotter.add_volume_clip_plane() widget to better see the internal structure of the dataset.
import numpy as np
import pyvista as pv
= pv.ImageData(dimensions=(200, 200, 200))
grid 'scalars'] = np.linalg.norm(grid.center - grid.points, axis=1)
grid[ grid
Header | Data Arrays | ||||||||||||||||||||||||||||||
|
|
= np.zeros(100)
opacity 10] = np.geomspace(0.01, 0.75, 10) opacity[::
= pv.Plotter()
pl ='-x', opacity=opacity[::-1], cmap='magma')
pl.add_volume_clip_plane(grid, normal pl.show()
import numpy as np
import pyvista
= np.random.default_rng(seed=0)
rng = rng.random((100, 3))
point_cloud = pyvista.PolyData(point_cloud)
pdata 'orig_sphere'] = np.arange(100)
pdata[
# create many spheres from the point cloud
= pyvista.Sphere(radius=0.02, phi_resolution=10, theta_resolution=10)
sphere = pdata.glyph(scale=False, geom=sphere, orient=False)
pc ='Reds') pc.plot(cmap
Performing boolean/topological operations (intersect, union, difference) methods are implemented for pyvista.PolyData mesh types only and are accessible directly from any pyvista.PolyData mesh.
import pyvista
import numpy as np
def make_cube():
= np.linspace(-0.5, 0.5, 25)
x = pyvista.StructuredGrid(*np.meshgrid(x, x, x))
grid = grid.extract_surface().triangulate()
surf
surf.flip_normals()return surf
# Create example PolyData meshes for boolean operations
= pyvista.Sphere(radius=0.65, center=(0, 0, 0))
sphere = make_cube()
cube
# Perform a boolean difference
= cube.boolean_difference(sphere)
boolean ='darkgrey', smooth_shading=True, split_sharp_edges=True) boolean.plot(color
Creating a spline/polyline from a numpy array of XYZ vertices using pyvista.Spline().
import numpy as np
import pyvista
# Make the xyz points
= np.linspace(-10 * np.pi, 10 * np.pi, 100)
theta = np.linspace(-2, 2, 100)
z = z**2 + 1
r = r * np.sin(theta)
x = r * np.cos(theta)
y = np.column_stack((x, y, z))
points
= pyvista.Spline(points, 500).tube(radius=0.1)
spline ='arc_length', show_scalar_bar=False) spline.plot(scalars
Here, we download the Stanford dragon mesh, color it according to height, and plot it using a web-viewer.
from pyvista import examples
= examples.download_dragon()
mesh 'scalars'] = mesh.points[:, 1]
mesh[='xy', cmap='plasma') mesh.plot(cpos