1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
"""
.. _slider_bar_widget_example:
Slider Bar Widget
~~~~~~~~~~~~~~~~~
The slider widget can be enabled and disabled by the
:func:`pyvista.Plotter.add_slider_widget` and
:func:`pyvista.Plotter.clear_slider_widgets` methods respectively.
This is one of the most versatile widgets as it can control a value that can
be used for just about anything.
"""
# sphinx_gallery_start_ignore
# widgets do not work in interactive examples
from __future__ import annotations
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
# sphinx_gallery_end_ignore
# sphinx_gallery_thumbnail_number = 1
##############################################################################
# One helper method we've added is the
# :func:`pyvista.Plotter.add_mesh_threshold` method which leverages the
# slider widget to control a thresholding value.
import pyvista as pv
from pyvista import examples
mesh = examples.download_knee_full()
p = pv.Plotter()
p.add_mesh_threshold(mesh)
p.show()
# %%
# After interacting with the scene, the threshold mesh is available as:
p.threshold_meshes
##############################################################################
# And here is a screen capture of a user interacting with this
#
# .. image:: ../../images/gifs/slider-widget-threshold.gif
# %%
# Custom Callback
# +++++++++++++++
#
# Or you could leverage a custom callback function that takes a single value
# from the slider as its argument to do something like control the resolution
# of a mesh. Again note the use of the ``name`` argument in ``add_mesh``:
p = pv.Plotter()
def create_mesh(value):
res = int(value)
sphere = pv.Sphere(phi_resolution=res, theta_resolution=res)
p.add_mesh(sphere, name='sphere', show_edges=True)
p.add_slider_widget(create_mesh, [5, 100], title='Resolution')
p.show()
##############################################################################
# And here is a screen capture of a user interacting with this
#
# .. image:: ../../images/gifs/slider-widget-resolution.gif
|