Using itkwidgets with PyVista#


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.


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)

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)



Return the background color of the plotter.


Return camera position of the plotter as a list.


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)[source]#

Bases: object

ITKwidgets plotter.

Used for plotting interactively within a jupyter notebook. Requires itkwidgets>=0.25.2. For installation see:


>>> import pyvista
>>> mesh = pyvista.Sphere()
>>> pl = pyvista.PlotterITK()  
>>> pl.add_mesh(mesh, color='w')  
>>> pl.background_color = 'k'  
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.

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.

colorcolor_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], 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)[source]#

Add points to plotter.

pointsnumpy.ndarray or pyvista.DataSet

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

colorcolor_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_sizefloat, optional

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


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.

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.


ITKwidgets viewer.