
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorial/02_mesh/exercises/c_create-uniform-grid.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_tutorial_02_mesh_exercises_c_create-uniform-grid.py>`
        to download the full example code. or to run this example in your browser via Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_tutorial_02_mesh_exercises_c_create-uniform-grid.py:


.. _create_uniform_grid_exercise:

Creating a Uniform Grid
~~~~~~~~~~~~~~~~~~~~~~~

Create a simple uniform grid from a 3D NumPy array of values.

.. GENERATED FROM PYTHON SOURCE LINES 10-14

.. code-block:: Python


    import numpy as np
    import pyvista as pv


.. GENERATED FROM PYTHON SOURCE LINES 15-19

Take a 3D NumPy array of data values that holds some spatial data where each
axis corresponds to the XYZ cartesian axes. This example will create a
:class:`pyvista.ImageData` that will hold the spatial reference for
a 3D grid by which a 3D NumPy array of values can be plotted against.

.. GENERATED FROM PYTHON SOURCE LINES 21-23

Create the 3D NumPy array of spatially referenced data. This is spatially
referenced such that the grid is ``(20, 5, 10)``, ``(nx, ny, nz)``.

.. GENERATED FROM PYTHON SOURCE LINES 23-26

.. code-block:: Python

    values = np.linspace(0, 10, 1000).reshape((20, 5, 10))
    values.shape


.. GENERATED FROM PYTHON SOURCE LINES 27-28

Create the ImageData

.. GENERATED FROM PYTHON SOURCE LINES 28-30

.. code-block:: Python

    grid = pv.ImageData()


.. GENERATED FROM PYTHON SOURCE LINES 31-33

Set the grid dimensions to ``shape + 1`` because we want to inject our values
on the CELL data.

.. GENERATED FROM PYTHON SOURCE LINES 33-35

.. code-block:: Python

    grid.dimensions = np.array(values.shape) + 1


.. GENERATED FROM PYTHON SOURCE LINES 36-37

Edit the spatial reference.

.. GENERATED FROM PYTHON SOURCE LINES 37-40

.. code-block:: Python

    grid.origin = (100, 33, 55.6)  # The bottom left corner of the data set
    grid.spacing = (1, 5, 2)  # These are the cell sizes along each axis


.. GENERATED FROM PYTHON SOURCE LINES 41-43

Assign the data to the cell data. Be sure to flatten the data for
``ImageData`` objects using Fortran ordering.

.. GENERATED FROM PYTHON SOURCE LINES 43-46

.. code-block:: Python

    grid.cell_data["values"] = values.flatten(order="F")
    grid


.. GENERATED FROM PYTHON SOURCE LINES 47-48

Now plot the grid!

.. GENERATED FROM PYTHON SOURCE LINES 48-51

.. code-block:: Python

    grid.plot(show_edges=True)



.. GENERATED FROM PYTHON SOURCE LINES 52-55

Don't like cell data? You could also add the NumPy array to the point data of
a :class:`pyvista.ImageData`. Take note of the subtle difference when
setting the grid dimensions upon initialization.

.. GENERATED FROM PYTHON SOURCE LINES 55-60

.. code-block:: Python


    # Create the 3D NumPy array of spatially referenced data again.
    values = np.linspace(0, 10, 1000).reshape((20, 5, 10))
    values.shape


.. GENERATED FROM PYTHON SOURCE LINES 61-62

Create the PyVista object and set the same attributes as earlier.

.. GENERATED FROM PYTHON SOURCE LINES 62-72

.. code-block:: Python

    grid = pv.ImageData()

    # Set the grid dimensions to ``shape`` because we want to inject our values on
    # the POINT data
    grid.dimensions = values.shape

    # Edit the spatial reference
    grid.origin = (100, 33, 55.6)  # The bottom left corner of the data set
    grid.spacing = (1, 5, 2)  # These are the cell sizes along each axis


.. GENERATED FROM PYTHON SOURCE LINES 73-74

Add the data values to the cell data

.. GENERATED FROM PYTHON SOURCE LINES 74-77

.. code-block:: Python

    grid.point_data["values"] = values.flatten(order="F")  # Flatten the array!
    grid


.. GENERATED FROM PYTHON SOURCE LINES 78-79

Now plot the grid!

.. GENERATED FROM PYTHON SOURCE LINES 79-82

.. code-block:: Python

    grid.plot(show_edges=True)



.. GENERATED FROM PYTHON SOURCE LINES 83-86

Exercise
^^^^^^^^
Now create your own :class:`pyvista.ImageData` from a 3D NumPy array!

.. GENERATED FROM PYTHON SOURCE LINES 86-88

.. code-block:: Python

    help(pv.ImageData)


.. GENERATED FROM PYTHON SOURCE LINES 89-91

Generate example 3D data using :func:`numpy.random.random`. Feel free to use
your own 3D numpy array here.

.. GENERATED FROM PYTHON SOURCE LINES 91-94

.. code-block:: Python

    arr = np.random.random((100, 100, 100))
    arr.shape


.. GENERATED FROM PYTHON SOURCE LINES 95-100

Create the :class:`pyvista.ImageData`.

.. note::
   You will likely need to ``ravel`` the array with Fortran-ordering:
   ``arr.ravel(order="F")``

.. GENERATED FROM PYTHON SOURCE LINES 100-104

.. code-block:: Python


    vol = pv.ImageData()
    # Set attributes and data


.. GENERATED FROM PYTHON SOURCE LINES 105-106

Plot the ImageData

.. GENERATED FROM PYTHON SOURCE LINES 106-109

.. code-block:: Python

    vol.plot()



.. GENERATED FROM PYTHON SOURCE LINES 110-118

Example
^^^^^^^
PyVista has several examples that use ``ImageData``.

See the PyVista documentation for further details on
`Volume Rendering <https://docs.pyvista.org/examples/02-plot/volume.html>`_

Here's one of these example datasets:

.. GENERATED FROM PYTHON SOURCE LINES 118-126

.. code-block:: Python

    from pyvista import examples

    vol = examples.download_knee_full()

    pl = pv.Plotter()
    pl.add_volume(vol, cmap="bone", opacity="sigmoid")
    pl.show()


.. GENERATED FROM PYTHON SOURCE LINES 127-130

.. code-block:: Python

    vol = pv.Wavelet()
    vol.plot(volume=True)


.. GENERATED FROM PYTHON SOURCE LINES 131-138

.. raw:: html

    <center>
      <a target="_blank" href="https://colab.research.google.com/github/pyvista/pyvista-tutorial/blob/gh-pages/notebooks/tutorial/02_mesh/exercises/c_create-uniform-grid.ipynb">
        <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/ width="150px">
      </a>
    </center>


.. _sphx_glr_download_tutorial_02_mesh_exercises_c_create-uniform-grid.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/pyvista/pyvista-tutorial/gh-pages?urlpath=lab/tree/notebooks/tutorial/02_mesh/exercises/c_create-uniform-grid.ipynb
        :alt: Launch binder
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: c_create-uniform-grid.ipynb <c_create-uniform-grid.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: c_create-uniform-grid.py <c_create-uniform-grid.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: c_create-uniform-grid.zip <c_create-uniform-grid.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
