| 1 |
""" |
| 2 |
.. _contouring_example: |
| 3 |
|
| 4 |
Contouring |
| 5 |
~~~~~~~~~~ |
| 6 |
|
| 7 |
Generate iso-lines or -surfaces for the scalars of a surface or volume. |
| 8 |
|
| 9 |
3D meshes can have 2D iso-surfaces of a scalar field extracted and 2D surface |
| 10 |
meshes can have 1D iso-lines of a scalar field extracted. |
| 11 |
""" |
| 12 |
|
| 13 |
import numpy as np |
| 14 |
import pyvista as pv |
| 15 |
from pyvista import examples |
| 16 |
|
| 17 |
# %% |
| 18 |
# Iso-Lines |
| 19 |
# +++++++++ |
| 20 |
# |
| 21 |
# Let's extract 1D iso-lines of a scalar field from a 2D surface mesh. |
| 22 |
mesh = examples.load_random_hills() |
| 23 |
|
| 24 |
contours = mesh.contour() |
| 25 |
|
| 26 |
pl = pv.Plotter() |
| 27 |
pl.add_mesh(mesh, opacity=0.85) |
| 28 |
pl.add_mesh(contours, color='white', line_width=5) |
| 29 |
pl.show() |
| 30 |
|
| 31 |
|
| 32 |
# %% |
| 33 |
# Iso-Surfaces |
| 34 |
# ++++++++++++ |
| 35 |
# |
| 36 |
# Let's extract 2D iso-surfaces of a scalar field from a 3D mesh. |
| 37 |
mesh = examples.download_embryo() |
| 38 |
|
| 39 |
contours = mesh.contour(np.linspace(50, 200, 5)) |
| 40 |
|
| 41 |
pl = pv.Plotter() |
| 42 |
pl.add_mesh(mesh.outline(), color='k') |
| 43 |
pl.add_mesh(contours, opacity=0.25, clim=[0, 200]) |
| 44 |
pl.camera_position = pv.CameraPosition( |
| 45 |
position=(-130.99381142132086, 644.4868354828589, 163.80447435848686), |
| 46 |
focal_point=(125.21748748157661, 123.94368717158413, 108.83283586619626), |
| 47 |
viewup=(0.2780372840777734, 0.03547871361794171, 0.9599148553609699), |
| 48 |
) |
| 49 |
pl.show() |
| 50 |
|
| 51 |
|
| 52 |
# %% |
| 53 |
# Banded Contours |
| 54 |
# +++++++++++++++ |
| 55 |
# Create banded contours for surface meshes using |
| 56 |
# :func:`~pyvista.PolyDataFilters.contour_banded`. |
| 57 |
mesh = examples.load_random_hills() |
| 58 |
|
| 59 |
# %% |
| 60 |
# Set number of contours and produce mesh and lines |
| 61 |
n_contours = 8 |
| 62 |
contours, edges = mesh.contour_banded(n_contours) |
| 63 |
|
| 64 |
# %% |
| 65 |
# Also make normal vectors |
| 66 |
arrows = mesh.glyph(scale='Normals', orient='Normals', tolerance=0.05) |
| 67 |
|
| 68 |
# %% |
| 69 |
|
| 70 |
# Common display arguments |
| 71 |
dargs = dict(scalars='Elevation', n_colors=n_contours - 1, cmap='Set3') |
| 72 |
|
| 73 |
pl = pv.Plotter() |
| 74 |
pl.add_mesh(edges, line_width=5, render_lines_as_tubes=True, color='k') |
| 75 |
pl.add_mesh(contours, **dargs) |
| 76 |
pl.add_mesh(arrows, **dargs) |
| 77 |
pl.show() |
| 78 |
|
| 79 |
# %% |
| 80 |
# Contours from a label map |
| 81 |
# +++++++++++++++++++++++++ |
| 82 |
# |
| 83 |
# Create labeled surfaces from 3D label maps (e.f. multi-label image segmentation) |
| 84 |
# using :func:`~pyvista.ImageDataFilters.contour_labels`. |
| 85 |
# Requires VTK version 9.3 |
| 86 |
if pv.vtk_version_info >= (9, 3): |
| 87 |
label_map = pv.examples.load_frog_tissues() |
| 88 |
mesh = label_map.contour_labels() |
| 89 |
mesh.plot(cmap='glasbey', cpos='yx', categories=True) |
| 90 |
# %% |
| 91 |
# .. tags:: filter |