Using itkwidgets
with PyVista#
Note
As of version 0.32.0
, itkwidgets
does not support
Jupyterlab 3. Attempting to run the following will return a
Model not found
error within jupyterlab.
Track the progress of this in Issue 405.
PyVista has an interface for visualizing plots in Jupyter. The
pyvista.PlotterITK
class allows you interactively visualize a mesh
within a jupyter notebook. For those who prefer plotting within
jupyter, this is an great way of visualizing using VTK
and
pyvista
.
Special thanks to @thewtex for the itkwidgets library.
Installation#
To use PlotterITK you’ll need to install itkwidgets>=0.25.2
.
Follow the installation steps here.
You can install everything with pip if you prefer not using conda, but be sure your juptyerlab is up-to-date. If you encounter problems, uninstall and reinstall jupyterlab using pip.
Example Plotting with ITKwidgets#
The following example shows how to create a simple plot that shows a simple sphere.
import pyvista as pv
# create a mesh and identify some scalars you wish to plot
mesh = pv.Sphere()
z = mesh.points[:, 2]
# Plot using the ITKplotter
pl = pv.PlotterITK()
pl.add_mesh(mesh, scalars=z, smooth_shading=True)
pl.show(True)

ITKwidgets with pyvista#
For convenience, figures can also be plotted using the plot_itk
function:
import pyvista as pv
# create a mesh and identify some scalars you wish to plot
mesh = pv.Sphere()
z = mesh.points[:, 2]
# Plot using the itkwidget
pv.plot_itk(mesh, scalars=z)
Attributes
Return the background color of the plotter. |
|
Return camera position of the plotter as a list. |
Methods
|
Add a PyVista/VTK mesh or dataset. |
|
Add points to plotter. |
|
Show itkwidgets plotter in cell output. |
- class PlotterITK(**kwargs)[source]#
Bases:
object
ITKwidgets plotter.
Used for plotting interactively within a jupyter notebook. Requires
itkwidgets>=0.25.2
. For installation see:https://github.com/InsightSoftwareConsortium/itkwidgets#installation
Examples
>>> import pyvista >>> mesh = pyvista.Sphere() >>> pl = pyvista.PlotterITK() >>> pl.add_mesh(mesh, color='w') >>> pl.background_color = 'k' >>> pl.show()
- add_mesh(mesh, color=None, scalars=None, opacity=1.0, smooth_shading=False)[source]#
Add a PyVista/VTK mesh or dataset.
Adds any PyVista/VTK mesh that itkwidgets can wrap to the scene.
- Parameters
- mesh
pyvista.DataSet
orpyvista.MultiBlock
Any PyVista or VTK mesh is supported. Also, any dataset that
pyvista.wrap()
can handle including NumPy arrays of XYZ points.- color
color_like
,optional
Use to make the entire mesh have a single solid color. Either a string, RGB list, or hex color string. For example:
color='white'
,color='w'
,color=[1.0, 1.0, 1.0]
, orcolor='#FFFFFF'
. Color will be overridden if scalars are specified.- scalars
str
ornumpy.ndarray
,optional
Scalars used to “color” the mesh. Accepts a string name of an array that is present on the mesh or an array equal to the number of cells or the number of points in the mesh. Array should be sized as a single vector. If both
color
andscalars
areNone
, then the active scalars are used.- opacity
float
,optional
Opacity of the mesh. If a single float value is given, it will be the global opacity of the mesh and uniformly applied everywhere - should be between 0 and 1. Default 1.0.
- smooth_shadingbool,
optional
Smooth mesh surface mesh by taking into account surface normals. Surface will appear smoother while sharp edges will still look sharp. Default
False
.
- mesh
- add_points(points, color=None, point_size=3.0)[source]#
Add points to plotter.
- Parameters
- points
numpy.ndarray
orpyvista.DataSet
An
n x 3
numpy array of points or PyVista dataset with points.- color
color_like
,optional
Either a string, RGB sequence, or hex color string. For one of the following.
color='white'
color='w'
color=[1.0, 1.0, 1.0]
color='#FFFFFF'
- point_size
float
,optional
Point size of any nodes in the dataset plotted. Also applicable when style=’points’. Default
3.0
.
- points
Examples
Add 10 random points to the plotter
>>> add_points(np.random.random((10, 3)), 'r', 10)
- property background_color#
Return the background color of the plotter.
- property camera_position#
Return camera position of the plotter as a list.
- show(ui_collapsed=True, rotate=False, show_bounds=False, **kwargs)[source]#
Show itkwidgets plotter in cell output.
- Parameters
- ui_collapsedbool,
optional
Plot with the user interface collapsed. UI can be enabled when plotting. Default
False
.- rotatebool,
optional
Rotate the camera around the scene. Default
False
. Appears to be computationally intensive.- show_boundsbool,
optional
Show the bounding box. Default
False
.- **kwargs
dict
,optional
Additional arguments to pass to
itkwidgets.Viewer
.
- ui_collapsedbool,
- Returns
itkwidgets.Viewer
ITKwidgets
viewer.