{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Common Filters {#common_filter_example}\n\nUsing common filters like thresholding and clipping.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista as pv\nfrom pyvista import examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "PyVista wrapped data objects have a suite of common filters ready for\nimmediate use directly on the object. These filters include the\nfollowing (see `filters`{.interpreted-text role=\"ref\"} for a complete\nlist):\n\n- `slice`: creates a single slice through the input dataset on a user\n defined plane\n- `slice_orthogonal`: creates a `MultiBlock` dataset of three\n orthogonal slices\n- `slice_along_axis`: creates a `MultiBlock` dataset of many slices\n along a specified axis\n- `threshold`: Thresholds a dataset by a single value or range of\n values\n- `threshold_percent`: Threshold by percentages of the scalar range\n- `clip`: Clips the dataset by a user defined plane\n- `outline_corners`: Outlines the corners of the data extent\n- `extract_geometry`: Extract surface geometry\n\nTo use these filters, call the method of your choice directly on your\ndata object:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dataset = examples.load_uniform()\ndataset.set_active_scalars(\"Spatial Point Data\")\n\n# Apply a threshold over a data range\nthreshed = dataset.threshold([100, 500])\n\noutline = dataset.outline()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now there is a thresholded version of the input dataset in the new\n`threshed` object. To learn more about what keyword arguments are\navailable to alter how filters are executed, print the docstring for any\nfilter attached to PyVista objects with either `help(dataset.threshold)`\nor using `shift+tab` in an IPython environment.\n\nWe can now plot this filtered dataset along side an outline of the\noriginal dataset\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\np.add_mesh(outline, color=\"k\")\np.add_mesh(threshed)\np.camera_position = [-2, 5, 3]\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What about other filters? Let\\'s collect a few filter results and\ncompare them:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "contours = dataset.contour()\nslices = dataset.slice_orthogonal()\nglyphs = dataset.glyph(factor=1e-3, geom=pv.Sphere())\n\np = pv.Plotter(shape=(2, 2))\n# Show the threshold\np.add_mesh(outline, color=\"k\")\np.add_mesh(threshed, show_scalar_bar=False)\np.camera_position = [-2, 5, 3]\n# Show the contour\np.subplot(0, 1)\np.add_mesh(outline, color=\"k\")\np.add_mesh(contours, show_scalar_bar=False)\np.camera_position = [-2, 5, 3]\n# Show the slices\np.subplot(1, 0)\np.add_mesh(outline, color=\"k\")\np.add_mesh(slices, show_scalar_bar=False)\np.camera_position = [-2, 5, 3]\n# Show the glyphs\np.subplot(1, 1)\np.add_mesh(outline, color=\"k\")\np.add_mesh(glyphs, show_scalar_bar=False)\np.camera_position = [-2, 5, 3]\n\np.link_views()\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Filter Pipeline\n\nIn VTK, filters are often used in a pipeline where each algorithm passes\nits output to the next filtering algorithm. In PyVista, we can mimic the\nfiltering pipeline through a chain; attaching each filter to the last\nfilter. In the following example, several filters are chained together:\n\n1. First, and empty `threshold` filter to clean out any `NaN` values.\n2. Use an `elevation` filter to generate scalar values corresponding to\n height.\n3. Use the `clip` filter to cut the dataset in half.\n4. Create three slices along each axial plane using the\n `slice_orthogonal` filter.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Apply a filtering chain\nresult = dataset.threshold().elevation().clip(normal=\"z\").slice_orthogonal()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And to view this filtered data, simply call the `plot` method\n(`result.plot()`) or create a rendering scene:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\np.add_mesh(outline, color=\"k\")\np.add_mesh(result, scalars=\"Elevation\")\np.view_isometric()\np.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }