In ParaView 4.1, the Python View was capable of generating visualizations using matplotlib only. We recently made some changes to the Python View to make it possible to use just about any Python library to generate images displayed in the view. The only restriction on Python libraries that can be used for this purpose is that there must be a way to access pixel data from a plot generated by the library. Matplotlib plots are still fully supported in a backwards-compatible way, so no changes to existing Python View scripts are required to use the now more versatile Python View
In a previous blog post, we detailed using the Python View to plot data loaded into ParaView. At a high-level view, the Python View requires two functions to be defined in the Python script. One function, called
setup_data(), is used to request data arrays in data objects that should be copied to the ParaView client. This step remains unchanged. The second function, called
render() has a new signature that makes it more widely useful. That change is the subject of the rest of this post.
render() function signature
render function had the signature
view was the Python View object of type vtkPythonView calling this function and
figure was a
matplotlib.figure.Figure object that was set up by the view automatically. The function was not expected to return a value.
render function has the signature
view is still the vtkPythonView object and the
height parameters are the width and height of the current render window in the view.
One of the most important changes to this function is that it is now expected to return a vtkImageData object containing the image content to display in the view render window. If no vtkImageData object is returned, the view window will be black.
Writing new Python View scripts that use matplotlib
No changes are necessary for Python View scripts used in ParaView 4.1 that define a
render() function that takes a
matplotlib.figure.Figure object as the second argument. However, for new matplotlib-based Python View scripts, we recommend using the new signature
render(view, width, height).
If you want to use matplotlib for plotting while using the new signature for
render(), your function needs to create a
matplotlib.figure.Figure object internally to do the plotting. This can be done with some utility code in the
figure object can be used in the same way as the
figure argument in the previous signature of the
After matplotlib plotting commands have been added, the
figure must be converted to a vtkImageData object. A convenience function has been added to the
python_view module to do this. It is used as follows:
The last step when using the new signature of the
render() function is to return the vtkImageData:
Pulling it all together, here is an example of a function with the new signature that generates a histogram of an array named 'X' associated with the point data in the first visible object in the pipeline browser:
Generating views with numpy
An example of a library that can be used to generate images to be displayed in the Python View is numpy. Images in numpy may be defined as 3D arrays (height, width, color components). The
python_view module provides a convenience function to convert numpy arrays to vtkImageData objects. As an example, the
render() function below generates a solid red image to display in the view.
This is not a terribly interesting use of the Python View, but it illustrates a minimal example of using numpy to generate content for display in the view window.
Using other plotting libraries
A collection of Python libraries that could potentially be used in the Python View can be found here. Note that aside from matplotlib, the libraries listed on this page have not been tested with the Python View. We would love to hear if you use any of these libraries in the Python View.