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.
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() model
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
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,) p.show()
[(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") p.show()
[(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, opacity_unit_distance=2000) p.camera_position = [(531554.5542909054, 3944331.800171338, 26563.04809259223), (599088.1433822059, 3982089.287834022, -11965.14728669936), (0.3738545892415734, 0.244312810377319, 0.8947312427698892)] p.show()
[(531554.5542909054, 3944331.800171338, 26563.04809259223), (599088.1433822059, 3982089.287834022, -11965.14728669936), (0.37385458924157344, 0.24431281037731903, 0.8947312427698894)]
|Tue Dec 29 01:56:53 2020 UTC|
|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]|
Total running time of the script: ( 0 minutes 11.362 seconds)