Point Cell Scalars#

This example demonstrates how to add point scalars for each individual cell to a dataset.

import numpy as np

from pyvista import examples

load the first 4 cells from the example UnstructuredGrid. Note how the number of points is less than 32 since all the points are joined in the center.

grid = examples.load_hexbeam().extract_cells(range(4))
grid
HeaderData Arrays
UnstructuredGridInformation
N Cells4
N Points18
X Bounds0.000e+00, 1.000e+00
Y Bounds0.000e+00, 1.000e+00
Z Bounds0.000e+00, 5.000e-01
N Arrays5
NameFieldTypeN CompMinMax
sample_point_scalarsPointsint6411.000e+002.860e+02
VTKorigIDPointsint6410.000e+009.000e+01
vtkOriginalPointIdsPointsint6410.000e+009.000e+01
sample_cell_scalarsCellsint3211.000e+004.000e+00
vtkOriginalCellIdsCellsint6410.000e+003.000e+00


Plot Point Scalars#

At this point it’s possible to assign only point or cell scalars to this dataset. First, let’s just plot some simple point scalars.

grid.point_data['Point Data'] = range(grid.n_points)
grid.plot(scalars='Point Data')
point cell scalars

Plot Cell Scalars#

Next, let’s plot cell scalars. We’re simply assigning based on the cell index.

grid.cell_data['Cell Data'] = range(grid.n_cells)
grid.plot(scalars='Cell Data')
point cell scalars

Splitting the Cells#

If you wanted to assign data to each point of each cell and plot that, it’s simply not possible since these hexahedral cells all share the same points. To split up individual cells, separate them using pyvista.DataSetFilters.separate_cells().

With this filter the resulting pyvista.UnstructuredGrid now contains 32 points, or 8 for each cell. They are now fully separated with no shared points.

split_cells = grid.separate_cells()
split_cells
HeaderData Arrays
UnstructuredGridInformation
N Cells4
N Points32
X Bounds0.000e+00, 1.000e+00
Y Bounds0.000e+00, 1.000e+00
Z Bounds0.000e+00, 5.000e-01
N Arrays7
NameFieldTypeN CompMinMax
sample_point_scalarsPointsint6411.000e+002.860e+02
VTKorigIDPointsint6410.000e+009.000e+01
vtkOriginalPointIdsPointsint6410.000e+009.000e+01
Point DataPointsint6410.000e+001.700e+01
sample_cell_scalarsCellsint3211.000e+004.000e+00
vtkOriginalCellIdsCellsint6410.000e+003.000e+00
Cell DataCellsint6410.000e+003.000e+00


Plot Point Cell Data#

Now we can plot values for each point for each cell. This will still be assigned to the point data.

Here we use numpy.hstack() for clarity, but as long as the length of the data matches the number of points, you’ll be able to use this approach.

See how the plotted values appear continuous within a cell and discontinuous between cells. This matches how stresses and strains are calculated from finite element solutions.

split_cells.point_data['Point Cell Data'] = np.hstack(
    (
        np.linspace(0, 8, 8),  # cell 0
        np.linspace(0, 12, 8),  # cell 1
        np.linspace(0, 16, 8),  # cell 2
        np.linspace(0, 20, 8),  # cell 3
    )
)
split_cells.plot(scalars='Point Cell Data')
point cell scalars

Total running time of the script: (0 minutes 0.872 seconds)

Gallery generated by Sphinx-Gallery