Gridded Data

Gridded datasets in pyvista capture vtkRectilinearGrid and vtkImageData data objects on the VTK backend. These data types have common features which are encompassed int their shared inheritance of pyvista.Grid

Attributes

dimensions

Return a length 3 tuple of the grid’s dimensions.

Methods

class pyvista.Grid(*args, **kwargs)

Bases: pyvista.core.common.Common

A class full of common methods for non-pointset grids.

property dimensions

Return a length 3 tuple of the grid’s dimensions.

These are effectively the number of nodes along each of the three dataset axes.

Rectilinear Grid

Attributes

points

Return a pointer to the points as a numpy object.

x

Get the coordinates along the X-direction.

y

Get the coordinates along the Y-direction.

z

Get the coordinates along the Z-direction.

Methods

save(filename[, binary])

Write a rectilinear grid to disk.

class pyvista.RectilinearGrid(*args, **kwargs)

Bases: vtkCommonDataModelPython.vtkRectilinearGrid, pyvista.core.grid.Grid

Extend the functionality of a vtk.vtkRectilinearGrid object.

Can be initialized in several ways:

  • Create empty grid

  • Initialize from a vtk.vtkRectilinearGrid object

  • Initialize directly from the point arrays

See _from_arrays in the documentation for more details on initializing from point arrays

Examples

>>> import pyvista
>>> import vtk
>>> import numpy as np
>>> # Create empty grid
>>> grid = pyvista.RectilinearGrid()
>>> # Initialize from a vtk.vtkRectilinearGrid object
>>> vtkgrid = vtk.vtkRectilinearGrid()
>>> grid = pyvista.RectilinearGrid(vtkgrid)
>>> # Create from NumPy arrays
>>> xrng = np.arange(-10, 10, 2)
>>> yrng = np.arange(-10, 10, 5)
>>> zrng = np.arange(-10, 10, 1)
>>> grid = pyvista.RectilinearGrid(xrng, yrng, zrng)
property points

Return a pointer to the points as a numpy object.

save(filename, binary=True)

Write a rectilinear grid to disk.

Parameters
  • filename (str) – Filename of grid to be written. The file extension will select the type of writer to use. “.vtk” will use the legacy writer, while “.vtr” will select the VTK XML writer.

  • binary (bool, optional) – Writes as a binary file by default. Set to False to write ASCII.

Notes

Binary files write much faster than ASCII, but binary files written on one system may not be readable on other systems. Binary can be used only with the legacy writer.

property x

Get the coordinates along the X-direction.

property y

Get the coordinates along the Y-direction.

property z

Get the coordinates along the Z-direction.

Uniform Grid

PyVista’s definition of a uniform grid is an extension of VTK’s vtkImageData

Attributes

origin

Return the origin of the grid (bottom southwest corner).

points

Return a pointer to the points as a numpy object.

spacing

Get the spacing for each axial direction.

x

Return all the X points.

y

Return all the Y points.

z

Return all the Z points.

Methods

cast_to_rectilinear_grid()

Cast this uniform grid to a pyvista.RectilinearGrid.

cast_to_structured_grid()

Cast this uniform grid to a pyvista.StructuredGrid.

save(filename[, binary])

Write image data grid to disk.

class pyvista.UniformGrid(*args, **kwargs)

Bases: vtkCommonDataModelPython.vtkImageData, pyvista.core.grid.Grid, pyvista.core.filters.UniformGridFilters

Extend the functionality of a vtk.vtkImageData object.

Can be initialized in several ways:

  • Create empty grid

  • Initialize from a vtk.vtkImageData object

  • Initialize directly from the point arrays

See _from_specs in the documentation for more details on initializing from point arrays

Examples

>>> import pyvista
>>> import vtk
>>> import numpy as np
>>> # Create empty grid
>>> grid = pyvista.UniformGrid()
>>> # Initialize from a vtk.vtkImageData object
>>> vtkgrid = vtk.vtkImageData()
>>> grid = pyvista.UniformGrid(vtkgrid)
>>> # Using just the grid dimensions
>>> dims = (10, 10, 10)
>>> grid = pyvista.UniformGrid(dims)
>>> # Using dimensions and spacing
>>> spacing = (2, 1, 5)
>>> grid = pyvista.UniformGrid(dims, spacing)
>>> # Using dimensions, spacing, and an origin
>>> origin = (10, 35, 50)
>>> grid = pyvista.UniformGrid(dims, spacing, origin)
cast_to_rectilinear_grid()

Cast this uniform grid to a pyvista.RectilinearGrid.

cast_to_structured_grid()

Cast this uniform grid to a pyvista.StructuredGrid.

property origin

Return the origin of the grid (bottom southwest corner).

property points

Return a pointer to the points as a numpy object.

save(filename, binary=True)

Write image data grid to disk.

Parameters
  • filename (str) – Filename of grid to be written. The file extension will select the type of writer to use. “.vtk” will use the legacy writer, while “.vti” will select the VTK XML writer.

  • binary (bool, optional) – Writes as a binary file by default. Set to False to write ASCII.

Notes

Binary files write much faster than ASCII, but binary files written on one system may not be readable on other systems. Binary can be used only with the legacy writer.

property spacing

Get the spacing for each axial direction.

property x

Return all the X points.

property y

Return all the Y points.

property z

Return all the Z points.