| 1 |
""" |
| 2 |
.. _point_cell_scalars_example: |
| 3 |
|
| 4 |
Point Cell Scalars |
| 5 |
~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
This example demonstrates how to add point scalars for each individual cell to |
| 8 |
a dataset. |
| 9 |
|
| 10 |
|
| 11 |
""" |
| 12 |
|
| 13 |
import numpy as np |
| 14 |
from pyvista import examples |
| 15 |
|
| 16 |
# %% |
| 17 |
# load the first 4 cells from the example UnstructuredGrid. Note how the number |
| 18 |
# of points is less than 32 since all the points are joined in the center. |
| 19 |
grid = examples.load_hexbeam().extract_cells(range(4)) |
| 20 |
grid |
| 21 |
|
| 22 |
|
| 23 |
# %% |
| 24 |
# Plot Point Scalars |
| 25 |
# ~~~~~~~~~~~~~~~~~~ |
| 26 |
# At this point it's possible to assign only point or cell scalars to this |
| 27 |
# dataset. First, let's just plot some simple point scalars. |
| 28 |
|
| 29 |
grid.point_data['Point Data'] = range(grid.n_points) |
| 30 |
grid.plot(scalars='Point Data') |
| 31 |
|
| 32 |
|
| 33 |
# %% |
| 34 |
# Plot Cell Scalars |
| 35 |
# ~~~~~~~~~~~~~~~~~ |
| 36 |
# Next, let's plot cell scalars. We're simply assigning based on the cell |
| 37 |
# index. |
| 38 |
grid.cell_data['Cell Data'] = range(grid.n_cells) |
| 39 |
grid.plot(scalars='Cell Data') |
| 40 |
|
| 41 |
|
| 42 |
# %% |
| 43 |
# Splitting the Cells |
| 44 |
# ~~~~~~~~~~~~~~~~~~~ |
| 45 |
# If you wanted to assign data to each point of each cell and plot that, it's |
| 46 |
# simply not possible since these hexahedral cells all share the same |
| 47 |
# points. To split up individual cells, separate them using |
| 48 |
# :func:`pyvista.DataSetFilters.separate_cells`. |
| 49 |
# |
| 50 |
# With this filter the resulting :class:`pyvista.UnstructuredGrid` now contains |
| 51 |
# 32 points, or 8 for each cell. They are now fully separated with no shared |
| 52 |
# points. |
| 53 |
|
| 54 |
split_cells = grid.separate_cells() |
| 55 |
split_cells |
| 56 |
|
| 57 |
|
| 58 |
# %% |
| 59 |
# Plot Point Cell Data |
| 60 |
# ~~~~~~~~~~~~~~~~~~~~ |
| 61 |
# Now we can plot values for each point for each cell. This will still be |
| 62 |
# assigned to the point data. |
| 63 |
# |
| 64 |
# Here we use :func:`numpy.hstack` for clarity, but as long as the length of |
| 65 |
# the data matches the number of points, you'll be able to use this approach. |
| 66 |
# |
| 67 |
# See how the plotted values appear continuous within a cell and discontinuous |
| 68 |
# between cells. This matches how stresses and strains are calculated from |
| 69 |
# finite element solutions. |
| 70 |
|
| 71 |
split_cells.point_data['Point Cell Data'] = np.hstack( |
| 72 |
( |
| 73 |
np.linspace(0, 8, 8), # cell 0 |
| 74 |
np.linspace(0, 12, 8), # cell 1 |
| 75 |
np.linspace(0, 16, 8), # cell 2 |
| 76 |
np.linspace(0, 20, 8), # cell 3 |
| 77 |
), |
| 78 |
) |
| 79 |
split_cells.plot(scalars='Point Cell Data') |
| 80 |
# %% |
| 81 |
# .. tags:: plot |