Basic usage#

This section demonstrates how to use PyVista to reading and plotting 3D data using the pyvista.examples module and external files.

Tip

This section of the tutorial was adopted from the Basic API Usage chapter of the PyVista documentation.

Using Existing Data#

There are two main ways of getting data into PyVista: creating it yourself from scratch or loading the dataset from any one of the compatible file formats. Since we’re just starting out, let’s load a file.

If you have a dataset handy, like a surface model, point cloud, or VTK file, you can use that. If you don’t have something immediately available, PyVista has a variety of files you can download in its pyvista.examples.downloads module.

Here’s a very basic dataset you can download.

from pyvista import examples
dataset = examples.download_saddle_surface()
dataset
PolyDataInformation
N Cells5131
N Points2669
N Strips0
X Bounds-2.001e+01, 2.000e+01
Y Bounds-6.480e-01, 4.024e+01
Z Bounds-6.093e-01, 1.513e+01
N Arrays0

Note how this is a pyvista.PolyData, which is effectively a surface dataset containing points, lines, and/or faces. We can immediately plot this with:

dataset.plot(color='tan')
../../_images/index_2_01.png

This is a fairly basic plot. You can change how its plotted by adding parameters as show_edges=True or changing the color by setting color to a different value. All of this is described in PyVista’s API documentation in pyvista.plot(), but for now let’s take a look at another dataset. This one is a volumetric dataset.

dataset = examples.download_frog()
dataset
HeaderData Arrays
ImageDataInformation
N Cells31594185
N Points31960000
X Bounds0.000e+00, 4.990e+02
Y Bounds0.000e+00, 4.690e+02
Z Bounds0.000e+00, 2.025e+02
Dimensions500, 470, 136
Spacing1.000e+00, 1.000e+00, 1.500e+00
N Arrays1
NameFieldTypeN CompMinMax
MetaImagePointsuint810.000e+002.540e+02

This is a pyvista.ImageData, which is a dataset containing a uniform set of points with consistent spacing. When we plot this dataset, we have the option of enabling volumetric plotting, which plots individual cells based on the content of the data associated with those cells.

dataset.plot(volume=True)
../../_images/index_4_0.png

Note

One thing you might have noticed by now is that the plots here in the online tutorial may look slightly different than your plots depending on how you’re plotting them on your computer. This depends on your jupyter_backend, or if you’re even using a jupyter notebook. As you’re playing around with these examples, feel free to change settings like disabling (or enabling) notebook, or using a different plotting backend for displaying within your notebook (if applicable).

Read from a file#

You can read datasets directly from a file if you have access to it locally on your computer. This can be one of the many file formats that VTK supports, and many more that it doesn’t as PyVista can rely on libraries like meshio.

In the following example, we load VTK’s iron protein dataset ironProt.vtk from a file using pyvista.read().

import pyvista as pv
dataset = pv.read('ironProt.vtk')
dataset
HeaderData Arrays
ImageDataInformation
N Cells300763
N Points314432
X Bounds0.000e+00, 6.700e+01
Y Bounds0.000e+00, 6.700e+01
Z Bounds0.000e+00, 6.700e+01
Dimensions68, 68, 68
Spacing1.000e+00, 1.000e+00, 1.000e+00
N Arrays1
NameFieldTypeN CompMinMax
scalarsPointsuint810.000e+002.550e+02

This is again a pyvista.ImageData and we can plot it volumetrically with:

dataset.plot(volume=True)
../../_images/index_6_0.png

Lesson Material#

This is the notebook rendering of this page where you can interactively follow along with this lesson.

Basic Usage Lesson

Basic Usage Lesson

Exercises#

Download and Plot Examples

Download and Plot Examples

Solutions#

These are the solutions to the above examples.

Download and Plot Examples

Download and Plot Examples

Gallery generated by Sphinx-Gallery