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)
../../_images/itk_plotting_sphere.png

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

background_color

Return the background color of the plotter.

camera_position

Return camera position of the plotter as a list.

Methods

add_mesh(mesh[, color, scalars, opacity, ...])

Add a PyVista/VTK mesh or dataset.

add_points(points[, color, point_size])

Add points to plotter.

show([ui_collapsed, rotate, show_bounds])

Show itkwidgets plotter in cell output.

class PlotterITK(**kwargs)

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)

Add a PyVista/VTK mesh or dataset.

Adds any PyVista/VTK mesh that itkwidgets can wrap to the scene.

Parameters
meshpyvista.DataSet or pyvista.MultiBlock

Any PyVista or VTK mesh is supported. Also, any dataset that pyvista.wrap() can handle including NumPy arrays of XYZ points.

colorstr sequence, 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, 1, 1], or color='#FFFFFF'. Color will be overridden if scalars are specified.

scalarsstr or numpy.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 and scalars are None, then the active scalars are used.

opacityfloat, 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.

add_points(points, color=None, point_size=3.0)

Add points to plotter.

Parameters
pointsnumpy.ndarray or pyvista.DataSet

An n x 3 numpy array of points or PyVista dataset with points.

colorstr or sequence, optional

Either a string, RGB sequence, or hex color string. For one of the following.

  • color='white'

  • color='w'

  • color=[1, 1, 1]

  • color='#FFFFFF'

point_sizefloat, optional

Point size of any nodes in the dataset plotted. Also applicable when style=’points’. Default 3.0.

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)

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.

**kwargsdict, optional

Additional arguments to pass to itkwidgets.Viewer.

Returns
itkwidgets.Viewer

ITKwidgets viewer.