Volume Rendering

Volume render uniform mesh types like pyvista.UniformGrid or 3D NumPy arrays

# sphinx_gallery_thumbnail_number = 3
import pyvista as pv
from pyvista import examples

# Volume rendering is not supported with Panel yet
pv.rcParams["use_panel"] = False

# Download a volumetric dataset
vol = examples.download_knee_full()
vol
HeaderData Arrays
UniformGridInformation
N Cells10225800
N Points10368384
X Bounds0.000e+00, 1.497e+02
Y Bounds0.000e+00, 1.786e+02
Z Bounds0.000e+00, 2.000e+02
Dimensions208, 248, 201
Spacing7.230e-01, 7.230e-01, 1.000e+00
N Arrays1
NameFieldTypeN CompMinMax
SLCImagePointsuint810.000e+001.740e+02


Simple Volume Render

# A nice camera position
cpos = [(-381.74, -46.02, 216.54), (74.8305, 89.2905, 100.0), (0.23, 0.072, 0.97)]

vol.plot(volume=True, cmap="bone", cpos=cpos)
../../_images/sphx_glr_volume_001.png

Out:

[(-381.74, -46.02, 216.54), (74.8305, 89.2905, 100.0), (0.23011692910752757, 0.07203660389453036, 0.9704931358013119)]

Opacity Mappings

Or use the pyvista.BasePlotter.add_volume() method like below. Note that here we use a non-default opacity mapping to a sigmoid:

p = pv.Plotter()
p.add_volume(vol, cmap="bone", opacity="sigmoid")
p.camera_position = cpos
p.show()
../../_images/sphx_glr_volume_002.png

Out:

[(-381.74, -46.02, 216.54), (74.8305, 89.2905, 100.0), (0.23011692910752757, 0.07203660389453036, 0.9704931358013119)]

You can also use a custom opacity mapping

opacity = [0, 0, 0, 0.1, 0.3, 0.6, 1]

p = pv.Plotter()
p.add_volume(vol, cmap="viridis", opacity=opacity)
p.camera_position = cpos
p.show()
../../_images/sphx_glr_volume_003.png

Out:

[(-381.74, -46.02, 216.54), (74.8305, 89.2905, 100.0), (0.23011692910752757, 0.07203660389453036, 0.9704931358013119)]

Cool Volume Examples

Here are a few more cool volume rendering examples

head = examples.download_head()

p = pv.Plotter()
p.add_volume(head, cmap="cool", opacity="sigmoid_6")
p.camera_position = [(-228.0, -418.0, -158.0), (94.0, 122.0, 82.0), (-0.2, -0.3, 0.9)]
p.show()
../../_images/sphx_glr_volume_004.png

Out:

[(-228.0, -418.0, -158.0), (94.0, 122.0, 82.0), (-0.20628424925175867, -0.309426373877638, 0.928279121632914)]
bolt_nut = examples.download_bolt_nut()

p = pv.Plotter()
p.add_volume(bolt_nut, cmap="coolwarm", opacity="sigmoid_5")
p.show()
../../_images/sphx_glr_volume_005.png

Out:

[(206.821456176791, 233.321456176791, 204.821456176791), (34.5, 61.0, 32.5), (0.0, 0.0, 1.0)]
frog = examples.download_frog()

p = pv.Plotter()
p.add_volume(frog, cmap="viridis", opacity="sigmoid_6")
p.camera_position = [(929., 1067., -278.9),
                     (249.5, 234.5, 101.25),
                     (-0.2048, -0.2632, -0.9427)]
p.show()
../../_images/sphx_glr_volume_006.png

Out:

[(929.0, 1067.0, -278.9), (249.5, 234.5, 101.25), (-0.20481018239133267, -0.2632130859638611, -0.942746869825729)]

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

Gallery generated by Sphinx-Gallery