Datasets¶
Datasets are any spatially reference information and usually consist of
geometrical representations of a surface or volume in 3D space.
In VTK, this superclass is represented by the vtk.vtkDataSet
abstract class.
In VTK, datasets consist of geometry, topology, and attributes to which PyVista provides direct access:
Geometry is the collection of points and cells in 2D or 3D space.
Topology defines the structure of the dataset, or how the points are connected to each other to form a cells making a surface or volume.
Attributes are any data values that are associated to either the points or cells of the dataset
All of the following data types are listed subclasses of a dataset and share a
set of common functionality which we wrap into the base class
pyvista.Common
.
The Common Model¶
The pyvista.Common
class holds attributes that are common to all
spatially referenced datasets in PyVista.
This base class is analogous to VTK’s vtk.vtkDataSet
class.
Attributes
Return the active scalars as an array. |
|
[field, name]. |
|
Return the active scalar’s name. |
|
Return the active tensors array. |
|
[field, name]. |
|
Return the name of the active tensor array. |
|
Return the active vectors array. |
|
[field, name]. |
|
Return the name of the active vectors array. |
|
Return a list of array names for the dataset. |
|
Return a glyph representation of the active vector data as arrows. |
|
Return the bounding box of this dataset. |
|
Return vtkCellData as DataSetAttributes. |
|
Return the center of the bounding box. |
|
Return the range of the bounding box. |
|
Return the length of the diagonal of the bounding box. |
|
Return the number of arrays present in the dataset. |
|
Return the number of cells in the entire dataset. |
|
Return the number of points in the entire dataset. |
|
Return the number of cells. |
|
Return the number of points. |
|
Return vtkPointData as DataSetAttributes. |
|
Return a pointer to the points as a numpy object. |
|
Return the active texture coordinates on the points. |
|
Return a dictionary to hold compatible |
|
Return active vectors. |
|
Return the mesh volume. |
Methods
Get a new representation of this object as an |
|
Remove all arrays from point/cell/field data. |
|
Remove all cell arrays. |
|
Remove all point arrays. |
|
Clear the textures from this mesh. |
|
|
Copy pyvista meta data onto this object from another object. |
|
Find index of closest cell in this mesh to the given point. |
|
Find index of closest point in this mesh to the given point. |
|
Search both point, cell and field data for an array. |
|
Get the non-NaN min and max of a named array. |
|
Overwrite this mesh inplace with the new mesh’s geometries and data. |
|
Plot a vtk or numpy object. |
Make points double precision. |
|
|
Change array name by searching for the array then renaming it. |
|
Rotate mesh about the x-axis. |
|
Rotate mesh about the y-axis. |
|
Rotate mesh about the z-axis. |
|
Find the scalars by name and appropriately sets it as active. |
|
Find the tensors by name and appropriately sets it as active. |
|
Find the vectors by name and appropriately sets it as active. |
|
Compute a transformation in place using a 4x4 transform. |
|
Translate the mesh. |
-
class
pyvista.
Common
(*args, **kwargs)¶ Bases:
pyvista.core.filters.DataSetFilters
,pyvista.core.common.DataObject
Methods in common to spatially referenced objects.
-
property
active_scalars
¶ Return the active scalars as an array.
-
property
active_scalars_info
¶ [field, name].
- Type
Return the active scalar’s field and name
-
property
active_scalars_name
¶ Return the active scalar’s name.
-
property
active_tensors
¶ Return the active tensors array.
-
property
active_tensors_info
¶ [field, name].
- Type
Return the active tensor’s field and name
-
property
active_tensors_name
¶ Return the name of the active tensor array.
-
property
active_vectors
¶ Return the active vectors array.
-
property
active_vectors_info
¶ [field, name].
- Type
Return the active scalar’s field and name
-
property
active_vectors_name
¶ Return the name of the active vectors array.
-
property
array_names
¶ Return a list of array names for the dataset.
This makes sure to put the active scalars’ name first in the list.
-
property
arrows
¶ Return a glyph representation of the active vector data as arrows.
Arrows will be located at the points of the mesh and their size will be dependent on the length of the vector. Their direction will be the “direction” of the vector
- Returns
arrows – Active scalars represented as arrows.
- Return type
-
property
bounds
¶ Return the bounding box of this dataset.
The form is: (xmin,xmax, ymin,ymax, zmin,zmax).
-
cast_to_unstructured_grid
()¶ Get a new representation of this object as an
pyvista.UnstructuredGrid
.
-
property
cell_arrays
¶ Return vtkCellData as DataSetAttributes.
-
property
center
¶ Return the center of the bounding box.
-
clear_arrays
()¶ Remove all arrays from point/cell/field data.
-
clear_cell_arrays
()¶ Remove all cell arrays.
-
clear_point_arrays
()¶ Remove all point arrays.
-
clear_textures
()¶ Clear the textures from this mesh.
-
copy_meta_from
(ido)¶ Copy pyvista meta data onto this object from another object.
-
property
extent
¶ Return the range of the bounding box.
-
find_closest_cell
(point)¶ Find index of closest cell in this mesh to the given point.
- Parameters
point (iterable(float) or np.ndarray) – Length 3 coordinate of the point to query or a
numpy
array of coordinates.- Returns
index – Index or indices of the cell in this mesh that is closest to the given point.
- Return type
int or np.ndarray
Examples
Find nearest cell to a point on a sphere
>>> import pyvista >>> mesh = pyvista.Sphere() >>> index = mesh.find_closest_cell([0, 0, 0.5]) >>> index 59
Find the nearest cells to several random points. Note that
-1
indicates that the locator was not able to find a reasonably close cell.>>> import numpy as np >>> points = np.random.random((1000, 3)) >>> indices = mesh.find_closest_cell(points) >>> print(indices.shape) (1000,)
-
find_closest_point
(point, n=1)¶ Find index of closest point in this mesh to the given point.
If wanting to query many points, use a KDTree with scipy or another library as those implementations will be easier to work with.
See: https://github.com/pyvista/pyvista-support/issues/107
- Parameters
point (iterable(float)) – Length 3 coordinate of the point to query.
n (int, optional) – If greater than
1
, returns the indices of then
closest points.
- Returns
int
- Return type
the index of the point in this mesh that is closes to the given point.
-
get_array
(name, preference='cell', info=False)¶ Search both point, cell and field data for an array.
-
get_data_range
(arr=None, preference='cell')¶ Get the non-NaN min and max of a named array.
- Parameters
arr (str, np.ndarray, optional) – The name of the array to get the range. If None, the active scalars is used.
preference (str, optional) – When scalars is specified, this is the preferred array type to search for in the dataset. Must be either
'point'
,'cell'
, or'field'
.
-
property
length
¶ Return the length of the diagonal of the bounding box.
-
property
n_arrays
¶ Return the number of arrays present in the dataset.
-
property
n_cells
¶ Return the number of cells in the entire dataset.
-
property
n_points
¶ Return the number of points in the entire dataset.
-
property
number_of_cells
¶ Return the number of cells.
-
property
number_of_points
¶ Return the number of points.
-
overwrite
(mesh)¶ Overwrite this mesh inplace with the new mesh’s geometries and data.
- Parameters
mesh (vtk.vtkDataSet) – The overwriting mesh.
-
plot
(off_screen=None, full_screen=False, screenshot=None, interactive=True, cpos=None, window_size=None, show_bounds=False, show_axes=True, notebook=None, background=None, text='', return_img=False, eye_dome_lighting=False, volume=False, parallel_projection=False, use_ipyvtk=None, **kwargs)¶ Plot a vtk or numpy object.
- Parameters
item (vtk or numpy object) – VTK object or numpy array to be plotted.
off_screen (bool) – Plots off screen when True. Helpful for saving screenshots without a window popping up.
full_screen (bool, optional) – Opens window in full screen. When enabled, ignores window_size. Default False.
screenshot (str or bool, optional) –
Saves screenshot to file when enabled. See: help(pyvistanterface.Plotter.screenshot). Default disabled.
When True, takes screenshot and returns numpy array of image.
window_size (list, optional) – Window size in pixels. Defaults to [1024, 768]
show_bounds (bool, optional) – Shows mesh bounds when True. Default False. Alias
show_grid
also accepted.notebook (bool, optional) – When True, the resulting plot is placed inline a jupyter notebook. Assumes a jupyter console is active.
show_axes (bool, optional) – Shows a vtk axes widget. Enabled by default.
text (str, optional) – Adds text at the bottom of the plot.
volume (bool, optional) – Use the
add_volume
method for volume rendering.use_ipyvtk (bool, optional) – Use the
ipyvtk-simple
ViewInteractiveWidget
to visualize the plot within a juyterlab notebook.**kwargs (optional keyword arguments) – See help(Plotter.add_mesh) for additional options.
- Returns
cpos (list) – List of camera position, focal point, and view up.
img (numpy.ndarray) – Array containing pixel RGB and alpha. Sized: [Window height x Window width x 3] for transparent_background=False [Window height x Window width x 4] for transparent_background=True Returned only when screenshot enabled
-
property
point_arrays
¶ Return vtkPointData as DataSetAttributes.
-
property
points
¶ Return a pointer to the points as a numpy object.
-
points_to_double
()¶ Make points double precision.
-
rename_array
(old_name, new_name, preference='cell')¶ Change array name by searching for the array then renaming it.
-
rotate_x
(angle)¶ Rotate mesh about the x-axis.
- Parameters
angle (float) – Angle in degrees to rotate about the x-axis.
-
rotate_y
(angle)¶ Rotate mesh about the y-axis.
- Parameters
angle (float) – Angle in degrees to rotate about the y-axis.
-
rotate_z
(angle)¶ Rotate mesh about the z-axis.
- Parameters
angle (float) – Angle in degrees to rotate about the z-axis.
-
set_active_scalars
(name, preference='cell')¶ Find the scalars by name and appropriately sets it as active.
To deactivate any active scalars, pass
None
as thename
.
-
set_active_tensors
(name, preference='point')¶ Find the tensors by name and appropriately sets it as active.
To deactivate any active tensors, pass
None
as thename
.
-
set_active_vectors
(name, preference='point')¶ Find the vectors by name and appropriately sets it as active.
To deactivate any active vectors, pass
None
as thename
.
-
property
t_coords
¶ Return the active texture coordinates on the points.
-
property
textures
¶ Return a dictionary to hold compatible
vtk.vtkTexture
objects.When casting back to a VTK dataset or filtering this dataset, these textures will not be passed.
-
transform
(trans)¶ Compute a transformation in place using a 4x4 transform.
- Parameters
trans (vtk.vtkMatrix4x4, vtk.vtkTransform, or np.ndarray) – Accepts a vtk transformation object or a 4x4 transformation matrix.
-
translate
(xyz)¶ Translate the mesh.
- Parameters
xyz (list or np.ndarray) – Length 3 list or array.
-
property
vectors
¶ Return active vectors.
-
property
volume
¶ Return the mesh volume.
- Returns
volume – Total volume of the mesh.
- Return type
float
-
property