.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorial/04_filters/a_lesson_filters.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorial_04_filters_a_lesson_filters.py: .. _common_filter_example: Using Common Filters ~~~~~~~~~~~~~~~~~~~~ Using common filters like thresholding and clipping. .. GENERATED FROM PYTHON SOURCE LINES 9-13 .. code-block:: Python import pyvista as pv from pyvista import examples .. GENERATED FROM PYTHON SOURCE LINES 15-40 PyVista wrapped data objects have a suite of common filters ready for immediate use directly on the object. These filters include the following (see `filters_ref`_ for a complete list): * `slice()`_ creates a single slice through the input dataset on a user defined plane * `slice_orthogonal()`_: creates a :class:`pyvista.MultiBlock` dataset of three orthogonal slices * `slice_along_axis()`_: creates a :class:`pyvista.MultiBlock` dataset of many slices along a specified axis * `threshold()`_: Thresholds a dataset by a single value or range of values * `threshold_percent()`_: Threshold by percentages of the scalar range * `clip()`_: Clips the dataset by a user defined plane * `outline_corners()`_: Outlines the corners of the data extent * `extract_geometry()`_: Extract surface geometry .. _slice(): https://docs.pyvista.org/version/stable/api/core/_autosummary/pyvista.DataSetFilters.slice.html .. _slice_orthogonal(): https://docs.pyvista.org/version/stable/api/core/_autosummary/pyvista.DataSetFilters.slice_orthogonal.html .. _slice_along_axis(): https://docs.pyvista.org/version/stable/api/core/_autosummary/pyvista.DataSetFilters.slice_along_axis.html .. _threshold(): https://docs.pyvista.org/version/stable/api/core/_autosummary/pyvista.DataSetFilters.threshold.html .. _threshold_percent(): https://docs.pyvista.org/version/stable/api/core/_autosummary/pyvista.DataSetFilters.threshold_percent.html .. _clip(): https://docs.pyvista.org/version/stable/api/core/_autosummary/pyvista.DataSetFilters.clip.html .. _outline_corners(): https://docs.pyvista.org/version/stable/api/core/_autosummary/pyvista.DataSetFilters.outline_corners.html .. _extract_geometry(): https://docs.pyvista.org/version/stable/api/core/_autosummary/pyvista.DataSetFilters.extract_geometry.html .. _filters_ref: https://docs.pyvista.org/version/stable/api/core/filters To use these filters, call the method of your choice directly on your data object: .. GENERATED FROM PYTHON SOURCE LINES 40-49 .. code-block:: Python dataset = examples.load_uniform() dataset.set_active_scalars("Spatial Point Data") # Apply a threshold over a data range threshed = dataset.threshold([100, 500]) outline = dataset.outline() .. GENERATED FROM PYTHON SOURCE LINES 50-58 And now there is a thresholded version of the input dataset in the new ``threshed`` object. To learn more about what keyword arguments are available to alter how filters are executed, print the docstring for any filter attached to PyVista objects with either ``help(dataset.threshold)`` or using ``shift+tab`` in an IPython environment. We can now plot this filtered dataset along side an outline of the original dataset .. GENERATED FROM PYTHON SOURCE LINES 58-66 .. code-block:: Python p = pv.Plotter() p.add_mesh(outline, color="k") p.add_mesh(threshed) p.camera_position = [-2, 5, 3] p.show() .. image-sg:: /tutorial/04_filters/images/sphx_glr_a_lesson_filters_001.png :alt: a lesson filters :srcset: /tutorial/04_filters/images/sphx_glr_a_lesson_filters_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 67-68 What about other filters? Let's collect a few filter results and compare them: .. GENERATED FROM PYTHON SOURCE LINES 68-97 .. code-block:: Python contours = dataset.contour() slices = dataset.slice_orthogonal() glyphs = dataset.glyph(factor=1e-3, geom=pv.Sphere(), orient=False) p = pv.Plotter(shape=(2, 2)) # Show the threshold p.add_mesh(outline, color="k") p.add_mesh(threshed, show_scalar_bar=False) p.camera_position = [-2, 5, 3] # Show the contour p.subplot(0, 1) p.add_mesh(outline, color="k") p.add_mesh(contours, show_scalar_bar=False) p.camera_position = [-2, 5, 3] # Show the slices p.subplot(1, 0) p.add_mesh(outline, color="k") p.add_mesh(slices, show_scalar_bar=False) p.camera_position = [-2, 5, 3] # Show the glyphs p.subplot(1, 1) p.add_mesh(outline, color="k") p.add_mesh(glyphs, show_scalar_bar=False) p.camera_position = [-2, 5, 3] p.link_views() p.show() .. image-sg:: /tutorial/04_filters/images/sphx_glr_a_lesson_filters_002.png :alt: a lesson filters :srcset: /tutorial/04_filters/images/sphx_glr_a_lesson_filters_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 98-113 Filter Pipeline +++++++++++++++ In VTK, filters are often used in a pipeline where each algorithm passes its output to the next filtering algorithm. In PyVista, we can mimic the filtering pipeline through a chain; attaching each filter to the last filter. In the following example, several filters are chained together: 1. First, and empty ``threshold`` filter to clean out any ``NaN`` values. 2. Use an ``elevation`` filter to generate scalar values corresponding to height. 3. Use the ``clip`` filter to cut the dataset in half. 4. Create three slices along each axial plane using the ``slice_orthogonal`` filter. Apply a filtering chain .. GENERATED FROM PYTHON SOURCE LINES 113-115 .. code-block:: Python result = dataset.threshold().elevation().clip(normal="z").slice_orthogonal() .. GENERATED FROM PYTHON SOURCE LINES 116-118 And to view this filtered data, simply call the ``plot`` method (``result.plot()``) or create a rendering scene: .. GENERATED FROM PYTHON SOURCE LINES 118-125 .. code-block:: Python p = pv.Plotter() p.add_mesh(outline, color="k") p.add_mesh(result, scalars="Elevation") p.view_isometric() p.show() .. image-sg:: /tutorial/04_filters/images/sphx_glr_a_lesson_filters_003.png :alt: a lesson filters :srcset: /tutorial/04_filters/images/sphx_glr_a_lesson_filters_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 126-133 .. raw:: html
Open In Colab
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.689 seconds) .. _sphx_glr_download_tutorial_04_filters_a_lesson_filters.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/pyvista/pyvista-tutorial/gh-pages?urlpath=lab/tree/notebooks/tutorial/04_filters/a_lesson_filters.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: a_lesson_filters.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: a_lesson_filters.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_