Debian Codegraph @

examples/03-widgets/slider_bar_widget.py

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