1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
|
.. _pyvista_data_model:
PyVista Data Model
==================
This section of the user guide explains in detail how to construct
meshes from scratch and to utilize the underlying VTK data model but
using the PyVista framework. Many of our :ref:`examples` simply
load data from files, but don't explain how to construct meshes or
place data within datasets.
.. note::
Though the following documentation section references VTK, it does
not require that you have knowledge of VTK. For those who wish to
see a detailed comparison to VTK or translate code written for the
Python bindings of VTK to PyVista, please see
:ref:`vtk_to_pyvista_docs`.
For a more general description of our API, see :ref:`what_is_a_mesh`.
The PyVista DataSet
-------------------
To visualize data in VTK or PyVista, two pieces of information are
required: the data's geometry, which describes where the data is
positioned in space and what its values are, and its topology, which
describes how points in the dataset are connected to one another.
At the top level, we have :vtk:`vtkDataObject`, which are just "blobs" of
data without geometry or topology. These contain arrays of
:vtk:`vtkFieldData`. Under this are :vtk:`vtkDataSet`, which add geometry and
topology to :vtk:`vtkDataObject`. Associated with every point or cell in
the dataset is a specific value. Since these values must be positioned
and connected in space, they are held in the :vtk:`vtkDataArray` class,
which are simply memory buffers on the heap. In PyVista, 99% of the
time we interact with :vtk:`vtkDataSet` objects rather than with
:vtk:`vtkDataObject` objects. PyVista uses the same data types as VTK, but
structures them in a more pythonic manner for ease of use.
If you'd like some background for how VTK structures its data, see
`Introduction to VTK in Python by Kitware
<https://vimeo.com/32232190>`_, as well as the numerous code examples
on `Kitware's GitHub site
<https://kitware.github.io/vtk-examples/site/>`_. An excellent
introduction to mathematical concepts relevant to 3D modeling in
general implemented in VTK is provided by the `Discrete Differential
Geometry YouTube Series
<https://www.youtube.com/playlist?list=PL9_jI1bdZmz0hIrNCMQW1YmZysAiIYSSS>`_
by Prof. Keenan Crane at Carnegie Mellon. The concepts taught here
will help improve your understanding of why data sets are structured
the way they are in libraries like VTK.
At the most fundamental level, all PyVista geometry classes inherit
from the :ref:`dataset` class. A dataset has geometry, topology,
and attributes describing that geometry in the form of point, cell, or
field arrays.
Geometry in PyVista is represented as points and cells. For example,
consider a single cell within a |PolyData|:
.. pyvista-plot::
:context:
:include-source: false
:force_static:
import pyvista
pyvista.set_plot_theme('document')
pyvista.set_jupyter_backend('static')
points = [[.2, 0, 0], [1.3, 0, 0], [1, 1.2, 0], [0, 1, 0]]
cells = [4, 0, 1, 2, 3]
mesh = pyvista.PolyData(points, cells)
pl = pyvista.Plotter()
pl.add_mesh(mesh, show_edges=False)
pl.add_mesh(mesh.extract_feature_edges(), line_width=5, color='k')
pl.add_point_labels(mesh.points, [f'Point {i}' for i in range(4)], font_size=20, point_size=20)
pl.add_point_labels(mesh.center, ['Cell'], font_size=28)
pl.camera_position = 'xy'
pl.show()
We would need a way to describe the position of each of these points
in space, but we're limited to expressing the values themselves as
we've done above (lists of arrays with indices). VTK (and hence
PyVista) have multiple classes that represent different data
shapes. The most important dataset classes are shown below:
.. pyvista-plot::
:context:
:include-source: false
from pyvista import demos
demos.plot_datasets()
Here, the above datasets are ordered from most (5) to least complex
(1). That is, every dataset can be represented as an
|UnstructuredGrid|, but the
|UnstructuredGrid| class takes the most amount of
memory to store since they must account for every individual point and
cell . On the other hand, since :vtk:`vtkImageData`
(|ImageData|) is uniformly spaced, a few integers and
floats can describe the shape, so it takes the least amount of memory
to store.
This is because in |PolyData| or
|UnstructuredGrid|, points, and cells must be explicitly
defined. In other data types, such as |ImageData|,
the cells (and even points) are defined as an emergent property based
on the dimensionality of the grid.
To see this in practice, let's create the simplest surface represented
as a |PolyData|. First, we need to define our points.
Points and Arrays Within PyVista
--------------------------------
There are a variety of ways to create points within PyVista, and this section shows how to efficiently create an array of points by either:
* Wrapping a VTK array
* Using a :class:`numpy.ndarray`
* Or just using a :class:`list`
PyVista provides pythonic methods for all three approaches so you can
choose whatever is most efficient for you. If you're comfortable with
the VTK API, you can choose to wrap VTK arrays, but you may find that
using :class:`numpy.ndarray` is more convenient and avoids the looping
overhead in Python.
Wrapping a VTK Array
~~~~~~~~~~~~~~~~~~~~
Let's define points of a triangle. Using the VTK API, this can be
done with:
.. jupyter-execute::
>>> import vtk
>>> vtk_array = vtk.vtkDoubleArray()
>>> vtk_array.SetNumberOfComponents(3)
>>> vtk_array.SetNumberOfValues(9)
>>> vtk_array.SetValue(0, 0)
>>> vtk_array.SetValue(1, 0)
>>> vtk_array.SetValue(2, 0)
>>> vtk_array.SetValue(3, 1)
>>> vtk_array.SetValue(4, 0)
>>> vtk_array.SetValue(5, 0)
>>> vtk_array.SetValue(6, 0.5)
>>> vtk_array.SetValue(7, 0.667)
>>> vtk_array.SetValue(8, 0)
>>> print(vtk_array)
PyVista supports creating objects directly from the :vtk:`vtkDataArray`
class, but there's a better, and more pythonic alternative by using
:class:`numpy.ndarray`.
Using NumPy with PyVista
~~~~~~~~~~~~~~~~~~~~~~~~
You can create a `NumPy <https://numpy.org/>`_ points array with:
.. jupyter-execute::
>>> import numpy as np
>>> np_points = np.array([[0, 0, 0], [1, 0, 0], [0.5, 0.667, 0]])
>>> np_points
We use a :class:`numpy.ndarray` here so that PyVista directly "points"
the underlying C array to VTK. VTK already has APIs to directly read
in the C arrays from NumPy, and since VTK is written in C++,
everything from Python that is transferred over to VTK needs to be in a
format that VTK can process.
Should you wish to use VTK objects within PyVista, you can still do
this. In fact, using :func:`pyvista.wrap`, you can even get a numpy-like
representation of the data. For example:
.. jupyter-execute::
>>> import pyvista
>>> wrapped = pyvista.wrap(vtk_array)
>>> wrapped
Note that when wrapping the underlying VTK array, we actually perform
a shallow copy of the data. In other words, we pass the pointer from
the underlying C array to the :class:`numpy.ndarray`, meaning
that the two arrays are now efficiently linked (in NumPy terminology,
the returned array is a view into the underlying VTK data). This means
that we can change the array using numpy array indexing and have it
modified on the "VTK side".
.. jupyter-execute::
>>> wrapped[0, 0] = 10
>>> vtk_array.GetValue(0)
Or we can change the value from the VTK array and see it reflected in
the numpy wrapped array. Let's change the value back:
.. jupyter-execute::
>>> vtk_array.SetValue(0, 0)
>>> wrapped[0, 0]
Using Python Lists or Tuples
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PyVista supports the use of Python sequences (that is, a :class:`list` or
:class:`tuple`), and you could define your points using a nested list
of lists via:
.. jupyter-execute::
>>> points = [[0, 0, 0], [1, 0, 0], [0.5, 0.667, 0]]
When used in the context of |PolyData| to create the
mesh, this list will automatically be wrapped using NumPy and then
passed to VTK. This avoids any looping overhead and while still
allowing you to use native python classes.
Finally, let's show how we can use these three objects in the context
of a PyVista geometry class. Here, we create a simple point mesh
containing just the three points:
.. jupyter-execute::
>>> from_vtk = pyvista.PolyData(vtk_array)
>>> from_np = pyvista.PolyData(np_points)
>>> from_list = pyvista.PolyData(points)
These point meshes all contain three points and are effectively
identical. Let's show this by accessing the underlying points array
from the mesh, which is represented as a :class:`pyvista.pyvista_ndarray`
.. jupyter-execute::
>>> from_vtk.points
And show that these are all identical
.. jupyter-execute::
>>> assert np.array_equal(from_vtk.points, from_np.points)
>>> assert np.array_equal(from_vtk.points, from_list.points)
>>> assert np.array_equal(from_np.points, from_list.points)
Finally, let's plot this (very) simple example using PyVista's
:func:`pyvista.plot` method. Let's make this a full example so you
can see the entire process.
.. pyvista-plot::
:context:
>>> import pyvista
>>> points = [[0, 0, 0], [1, 0, 0], [0.5, 0.667, 0]]
>>> mesh = pyvista.PolyData(points)
>>> mesh.plot(show_bounds=True, cpos='xy', point_size=20)
We'll get into PyVista's data classes and attributes later, but for
now we've shown how to create a simple geometry containing just points.
To create a surface, we must specify the connectivity of the geometry,
and to do that we need to specify the cells (or faces) of this surface.
Geometry and Mesh Connectivity/Topology Within PyVista
------------------------------------------------------
With our previous example, we defined our "mesh" as three disconnected
points. While this is useful for representing "point clouds," if we
want to create a surface, we have to describe the connectivity of the
mesh. To do this, let's define a single cell composed of three points
in the same order as we defined earlier.
.. jupyter-execute::
>>> cells = [3, 0, 1, 2]
.. note::
Observe how we had to insert a leading ``3`` to tell VTK that our
face is described by three elements, in this case, three points. In our |PolyData| VTK
doesn't assume that faces always contain three points, so we have
to define that. This actually gives us the flexibility to define
as many (or as few as one) points per cell as we wish.
.. note::
All cell types follow the same connectivity array format:
``[Number of points, Point 1, Point 2, ...]``
Except for ``polyhedron`` type, in which we need to define each face of the cell. The
format for this type is the following:
``[Number of elements, Number of faces, Face1NPoints, Point1, Point2, ..., PointN, Face2NPoints, ...]``.
Where ``number of elements`` is the total number of elements in the array that describe this cell.
Now we have all the necessary pieces to assemble an instance of
|PolyData| that contains a single triangle. To do
this, we simply provide the ``points`` and ``cells`` to the
constructor of a |PolyData|. We can see from the
representation that this geometry contains three points and one cell
.. jupyter-execute::
>>> mesh = pyvista.PolyData(points, cells)
>>> mesh
Let's also plot this:
.. pyvista-plot::
:context:
>>> mesh = pyvista.PolyData(points, [3, 0, 1, 2])
>>> mesh.plot(cpos='xy', show_edges=True)
While we're at it, let's annotate this plot to describe this mesh.
.. pyvista-plot::
:context:
:force_static:
>>> pl = pyvista.Plotter()
>>> pl.add_mesh(mesh, show_edges=True, line_width=5)
>>> label_coords = mesh.points + [0, 0, 0.01]
>>> pl.add_point_labels(
... label_coords,
... [f'Point {i}' for i in range(3)],
... font_size=20,
... point_size=20,
... )
>>> pl.add_point_labels([0.43, 0.2, 0], ['Cell 0'], font_size=20)
>>> pl.camera_position = 'xy'
>>> pl.show()
You can clearly see how the polygon is created based on the
connectivity of the points.
This instance has several attributes to access the underlying data of
the mesh. For example, if you wish to access or modify the points of
the mesh, you can simply access the points attribute with
:attr:`points <pyvista.DataSet.points>`.
.. jupyter-execute::
>>> mesh.points
The connectivity can also be accessed from the :attr:`faces <pyvista.PolyData.faces>`
attribute with:
.. jupyter-execute::
>>> mesh.faces
Or we could simply get the representation of the mesh with:
.. jupyter-execute::
>>> mesh
In this representation we see:
* Number of cells :attr:`n_cells <pyvista.DataSet.n_cells>`
* Number of points :attr:`n_points <pyvista.DataSet.n_points>`
* Bounds of the mesh :attr:`bounds <pyvista.DataSet.bounds>`
* Number of data arrays :attr:`n_arrays <pyvista.DataSet.n_arrays>`
This is vastly different from the output from VTK. See
:ref:`vtk_vs_pyvista_object_repr` for the comparison between the two
representations.
This mesh contains no data arrays as it consists only of geometry. This
makes it useful for plotting just the geometry of the mesh, but
datasets often contain more than just geometry. For example:
* An electrical field computed from a changing magnetic field
* Vector field of blood flow through artery
* Surface stresses from a structural finite element analysis
* Mineral deposits from geophysics
* Weather patterns as a vector field or surface data.
While each one of these datasets could be represented as a different
geometry class, they would all contain point, cell, or field data that
explains the value of the data at a certain location within the
geometry.
Data Arrays
-----------
Each :class:`DataSet <pyvista.DataSet>` contains
attributes that allow you to access the underlying numeric data. This
numerical data may be associated with the :attr:`points
<pyvista.DataSet.points>`, cells, or not associated with points
or cells and attached to the mesh in general.
To illustrate data arrays within PyVista, let's first construct a
slightly more complex mesh than our previous example. Here, we create
a simple mesh containing four isometric cells by starting with a
|ImageData| and then casting it to an |UnstructuredGrid| with
:func:`cast_to_unstructured_grid()
<pyvista.DataSet.cast_to_unstructured_grid>`.
.. jupyter-execute::
>>> grid = pyvista.ImageData(dimensions=(3, 3, 1))
>>> ugrid = grid.cast_to_unstructured_grid()
>>> ugrid
Let's also plot this basic mesh:
.. pyvista-plot::
:context:
:include-source: False
>>> grid = pyvista.ImageData(dimensions=(3, 3, 1))
>>> ugrid = grid.cast_to_unstructured_grid()
.. pyvista-plot::
:context:
:force_static:
>>> pl = pyvista.Plotter()
>>> pl.add_mesh(ugrid, show_edges=True, line_width=5)
>>> label_coords = ugrid.points + [0, 0, 0.02]
>>> point_labels = [f'Point {i}' for i in range(ugrid.n_points)]
>>> pl.add_point_labels(
... label_coords, point_labels, font_size=25, point_size=20
... )
>>> cell_labels = [f'Cell {i}' for i in range(ugrid.n_cells)]
>>> pl.add_point_labels(ugrid.cell_centers(), cell_labels, font_size=25)
>>> pl.camera_position = 'xy'
>>> pl.show()
Now that we have a simple mesh to work with, we can start assigning it
data. There are two main types of data that can be associated with a
mesh: scalar data and vector data. Scalar data is single or
multi-component data that is non directional and may include values
like temperature, or in the case of multi-component data, RGBA values.
Vector data has magnitude and direction and is represented as
arrays containing three components per data point.
When plotting, we can easily display scalar data, but this data must
be "associated" with either points or cells. For example, we may wish
to assign values to the cells of our example mesh, which we can do by
accessing the :attr:`cell_data <pyvista.DataSet.cell_data>`
attribute of our mesh.
.. _pyvista_data_model_cell_data:
Cell Data
~~~~~~~~~
The easiest way to add scalar data to a :class:`DataSet
<pyvista.DataSet>` is to use the ``[]`` operator.
Continuing with our example above, let's assign each cell a single
integer. We can do this using a Python :class:`list` and making it
the same length as the number of cells in the
|UnstructuredGrid|. Or as an even simpler example, using a
:class:`range` of the appropriate length. Here we create the range, add
it to the :attr:`cell_data <pyvista.DataSet.cell_data>`, and then access
it using the ``[]`` operator.
.. jupyter-execute::
>>> simple_range = range(ugrid.n_cells)
>>> ugrid.cell_data['my-data'] = simple_range
>>> ugrid.cell_data['my-data']
Note how we are returned a :class:`pyvista.pyvista_ndarray`. Since
VTK requires C arrays, PyVista will internally wrap or convert all
inputs to C arrays. We can then plot this with:
.. pyvista-plot::
:context:
:include-source: False
>>> ugrid.cell_data['my-data'] = range(ugrid.n_cells)
.. pyvista-plot::
:context:
>>> ugrid.plot(cpos='xy', show_edges=True)
Note how we did not have to specify which cell data to plot as the
``[]`` operator automatically sets the active scalars:
.. jupyter-execute::
>>> ugrid.cell_data
We can also add labels to our plot to show which cells are assigned
which scalars. Note how this is in the same order as the scalars we
assigned.
.. pyvista-plot::
:context:
:force_static:
>>> pl = pyvista.Plotter()
>>> pl.add_mesh(ugrid, show_edges=True, line_width=5)
>>> cell_labels = [f'Cell {i}' for i in range(ugrid.n_cells)]
>>> pl.add_point_labels(ugrid.cell_centers(), cell_labels, font_size=25)
>>> pl.camera_position = 'xy'
>>> pl.show()
We can continue to assign cell data to our :class:`DataSet
<pyvista.DataSet>` using the ``[]`` operator, but if you
do not wish the new array to become the active array, you can add it
using :func:`set_array() <pyvista.DataSetAttributes.set_array>`
.. jupyter-execute::
>>> data = np.linspace(0, 1, ugrid.n_cells)
>>> ugrid.cell_data.set_array(data, 'my-cell-data')
>>> ugrid.cell_data
Now, ``ugrid`` contains two arrays, one of which is the "active"
scalars. This set of active scalars will be the one plotted
automatically when ``scalars`` is unset in either :func:`add_mesh()
<pyvista.Plotter.add_mesh>` or :func:`pyvista.plot`. This makes it
possible to have many cell arrays associated with a dataset and
track which one will plotted as the active cell scalars by default.
The active scalars can also be accessed via
:attr:`active_scalars <pyvista.DataSet.active_scalars>`,
and the name of the active scalars array can be accessed or set with
:attr:`active_scalars_name
<pyvista.DataSet.active_scalars_name>`.
.. jupyter-execute::
>>> ugrid.cell_data.active_scalars_name = 'my-cell-data'
>>> ugrid.cell_data
Point Data
~~~~~~~~~~
Data can be associated to points in the same manner as in
:ref:`pyvista_data_model_cell_data`. The :attr:`point_data
<pyvista.DataSet.point_data>` attribute allows you to associate point
data to the points of a :class:`DataSet
<pyvista.DataSet>`. Here, we will associate a simple
list to the points using the ``[]`` operator.
.. jupyter-execute::
>>> simple_list = list(range(ugrid.n_points))
>>> ugrid.point_data['my-data'] = simple_list
>>> ugrid.point_data['my-data']
Again, these values become the active scalars in our point arrays by
default by using the ``[]`` operator:
.. jupyter-execute::
>>> ugrid.point_data
Let's plot the point data. Note how this varies from the cell data
plot; each individual point is assigned a scalar value which is
interpolated across a cell to create a smooth color map between the
lowest value at ``Point 0`` to the highest value at ``Point 8``.
.. pyvista-plot::
:context:
:include-source: False
We need this here since we have to update the pyvista-plot context.
>>> ugrid.point_data['my-data'] = range(ugrid.n_points)
.. pyvista-plot::
:context:
:force_static:
>>> pl = pyvista.Plotter()
>>> pl.add_mesh(ugrid, show_edges=True, line_width=5)
>>> label_coords = ugrid.points + [0, 0, 0.02]
>>> point_labels = [f'Point {i}' for i in range(ugrid.n_points)]
>>> pl.add_point_labels(
... label_coords, point_labels, font_size=25, point_size=20
... )
>>> pl.camera_position = 'xy'
>>> pl.show()
As in :ref:`pyvista_data_model_cell_data`, we can assign multiple
arrays to :attr:`point_data <pyvista.DataSet.point_data>`
using :func:`set_array() <pyvista.DataSetAttributes.set_array>`.
.. jupyter-execute::
>>> data = np.linspace(0, 1, ugrid.n_points)
>>> ugrid.point_data.set_array(data, 'my-point-data')
>>> ugrid.point_data
Again, here there are now two arrays associated to the point data, and
only one is the "active" scalars array. Like as in the cell data, we
can retrieve this with :attr:`active_scalars
<pyvista.DataSet.active_scalars>`, and the name of the
active scalars array can be accessed or set with
:attr:`active_scalars_name
<pyvista.DataSet.active_scalars_name>`.
.. jupyter-execute::
>>> ugrid.point_data.active_scalars_name = 'my-point-data'
>>> ugrid.point_data
Dataset Active Scalars
~~~~~~~~~~~~~~~~~~~~~~
Continuing from the previous sections, our ``ugrid`` dataset now
contains both point and cell data:
.. jupyter-execute::
>>> ugrid.point_data
.. jupyter-execute::
>>> ugrid.cell_data
There are active scalars in both point and cell data, but only one
type of scalars can be "active" at the dataset level. The reason for
this is that only one scalar type (be it point or cell) can be plotted
at once, and this data can be obtained from :attr:`active_scalars_info
<pyvista.DataSet.active_scalars_info>`:
.. jupyter-execute::
>>> ugrid.active_scalars_info
Note that the active scalars are by default the point scalars. You
can change this by setting the active scalars with
:func:`set_active_scalars()
<pyvista.DataSet.set_active_scalars>`. Note that if you
want to set the active scalars and both the point and cell data have
an array of the same name, you must specify the ``preference``:
.. jupyter-execute::
>>> ugrid.set_active_scalars('my-data', preference='cell')
>>> ugrid.active_scalars_info
This can also be set when plotting using the ``preference``
parameter in :func:`add_mesh() <pyvista.Plotter.add_mesh>` or
:func:`pyvista.plot`.
Field Data
~~~~~~~~~~
Field arrays are different from :attr:`point_data
<pyvista.DataSet.point_data>` and :attr:`cell_data
<pyvista.DataSet.cell_data>` in that they are not associated with
the geometry of the :class:`DataSet <pyvista.DataSet>`.
This means that while it's not possible to designate the field data as
active scalars or vectors, you can use it to "attach" arrays of any
shape. You can even add string arrays in the field data:
.. jupyter-execute::
>>> ugrid.field_data['my-field-data'] = ['hello', 'world']
>>> ugrid.field_data['my-field-data']
Note that the field data is automatically transferred to VTK C-style
arrays and then represented as a numpy data format.
When listing the current field data, note that the association is "NONE":
.. jupyter-execute::
>>> ugrid.field_data
This is because the data is not associated with points or cells, and
cannot be made so because field data is not expected to match the
number of cells or points. As such, it also cannot be plotted.
Vectors, Texture Coords, and Normals Attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Both cell and point data can also store the following "special" attributes in addition to :attr:`active_scalars <pyvista.DataSet.active_scalars>`:
* :attr:`active_normals <pyvista.DataSet.active_normals>`
* :attr:`active_texture_coordinates <pyvista.DataSet.active_texture_coordinates>`
* :attr:`active_vectors <pyvista.DataSet.active_vectors>`
Active Normals
~~~~~~~~~~~~~~
The :attr:`active_normals
<pyvista.DataSet.active_normals>` array is a special array that
specifies the local normal direction of meshes. It is used for
creating physically based rendering, rendering smooth shading using
Phong interpolation, warping by scalars, etc. If this array
is not set when plotting with ``smooth_shading=True`` or ``pbr=True``,
it will be computed.
Active Texture Coordinates
~~~~~~~~~~~~~~~~~~~~~~~~~~
The :attr:`active_texture_coordinates
<pyvista.DataSet.active_texture_coordinates>` array is used for
rendering textures. See :ref:`texture_example` for examples using
this array.
Active Vectors
~~~~~~~~~~~~~~
The :attr:`active_vectors
<pyvista.DataSet.active_vectors>` is an array containing
quantities that have magnitude and direction (specifically, three
components). For example, a vector field containing the wind speed at
various coordinates. This differs from :attr:`active_scalars
<pyvista.DataSet.active_scalars>` as scalars are expected
to be non-directional even if they contain several components (as in
the case of RGB data).
Vectors are treated differently within VTK than scalars when performing
transformations using the :func:`~pyvista.DataObjectFilters.transform`
filter. Unlike scalar arrays, vector arrays will be transformed along
with the geometry as these vectors represent quantities with direction.
.. note::
VTK permits only one "active" vector. If you have multiple vector
arrays that you wish to transform, set ``transform_all_input_vectors=True``
in :func:`~pyvista.DataObjectFilters.transform`. Be aware that this
will transform any array with three components, so multi-component
scalar arrays like RGB arrays will have to be discarded after
transformation.
.. |PolyData| replace:: :class:`PolyData <pyvista.PolyData>`
.. |UnstructuredGrid| replace:: :class:`UnstructuredGrid <pyvista.UnstructuredGrid>`
.. |ImageData| replace:: :class:`ImageData <pyvista.ImageData>`
|