Intro to PyVista

I don’t have all that many posts in the gallery yet, so I thought I’d put this very basic introduction to PyVista in here.

PyVista is a 3D data management, visualization, and mesh analysis framework in Python, and yours truly is one of its creators! PyVista is capable of handling 3D spatial data structures with arbitrary geometries. As such, it is able to represent just about any 3D spatially referenced data set, geoscientific or not!

If you really want to learn more about PyVista, head over to its gallery (I have created many of those examples).

In this brief demo, I’m going to load some example data and demonstrate how to plot and filter the 3D meshes.

Getting to it

So what is a “3D spatially referenced data”?…

In PyVista, a “mesh” is a geometrical representation of a surface or volume in 3D space that can have any information associated to those locations/regions in space. We commonly refer to any spatially referenced dataset as a mesh, so often the distinction between a mesh, a grid, and a volume can get fuzzy - but that does not matter in PyVista as we have worked really hard to make working with these spatial data structures as simple as possible. For more details, check out: What is a mesh?

Anyways, let’s look at some code and especially some 3D visualizations!

import pyvista as pv
from pyvista import examples

# Load OMF archive as PyVista MulitBlock dataset
model = examples.download_damavand_volcano()
HeaderData Arrays
N Cells11003760
N Points11156040
X Bounds4.130e+05, 6.920e+05
Y Bounds3.864e+06, 4.096e+06
Z Bounds-5.479e+04, 5.302e+03
Dimensions280, 233, 171
Spacing1.000e+03, 1.000e+03, 3.535e+02
N Arrays1
NameFieldTypeN CompMinMax

Initial Inspection

Now we can go ahead and create a visualization of the 3D model of the region surrounding the Damavand Volcano. All we have to do is pass the mesh to the pyvista.Plotter’s add_mesh method with keyword arguments for how we want it displayed (e.g. color, opacity, etc.).

Below, I define an opacity mapping and colorbar limits:

opacity = [0, 0.75, 0, 0.75, 1.0]
clim = [0, 100]

Now I instantiate the Plotter and create the visualization.

p = pv.Plotter()
p.add_volume(model, cmap="magma", clim=clim,
             opacity=opacity, opacity_unit_distance=6000,)
000 intro to pyvista


[(962759.682893736, 4390399.944685736, 385481.9418857361),
 (552532.741008, 3980173.0028, -24745.0),
 (0.0, 0.0, 1.0)]

And just like that, we have a 3D rendering of all that data! Isn’t PyVista awesome?!

But hold on, that’s a big volume! We probably don’t want to volume render the whole thing. So let’s extract a region of interest under the volcano.

The region we will extract will be between nodes 175 and 200 on the x-axis, between nodes 105 and 132 on the y-axis, and between nodes 98 and 170 on the z-axis.

voi = model.extract_subset([175, 200, 105, 132, 98, 170])

p = pv.Plotter()
p.add_mesh(model.outline(), color="k")
p.add_mesh(voi, cmap="magma")
000 intro to pyvista


[(962759.6918857361, 4390399.941885736, 385481.9418857361),
 (552532.75, 3980173.0, -24745.0),
 (0.0, 0.0, 1.0)]

Let’s now volume render that region of interest!

p = pv.Plotter()
p.add_volume(voi, cmap="magma", clim=clim, opacity=opacity,
p.camera_position = [(531554.5542909054, 3944331.800171338, 26563.04809259223),
 (599088.1433822059, 3982089.287834022, -11965.14728669936),
 (0.3738545892415734, 0.244312810377319, 0.8947312427698892)]
000 intro to pyvista


[(531554.5542909054, 3944331.800171338, 26563.04809259223),
 (599088.1433822059, 3982089.287834022, -11965.14728669936),
 (0.37385458924157344, 0.24431281037731903, 0.8947312427698894)]
Sun Mar 28 01:58:21 2021 UTC
OS Linux CPU(s) 2 Machine x86_64
Architecture 64bit Environment Python GPU Vendor VMware, Inc.
GPU Renderer llvmpipe (LLVM 6.0, 256 bits) GPU Version 3.3 (Core Profile) Mesa 18.0.5
Python 3.6.7 (default, Jun 12 2019, 02:05:21) [GCC 5.4.0 20160609]
pyvista 0.29.dev0 vtk 9.0.1 numpy 1.19.0
imageio 2.9.0 appdirs 1.4.4 scooby 0.5.6
meshio 4.3.11 matplotlib 3.3.4

Total running time of the script: ( 0 minutes 11.429 seconds)

Gallery generated by Sphinx-Gallery