.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorial/02_mesh/solutions/c_create-uniform-grid.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` 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_solutions_c_create-uniform-grid.py: .. _create_uniform_grid_solution: 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 .. rst-class:: sphx-glr-script-out .. code-block:: none (20, 5, 10) .. 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 .. raw:: html
ImageDataInformation
N Cells1000
N Points1386
X Bounds1.000e+02, 1.200e+02
Y Bounds3.300e+01, 5.800e+01
Z Bounds5.560e+01, 7.560e+01
Dimensions21, 6, 11
Spacing1.000e+00, 5.000e+00, 2.000e+00
N Arrays1
NameFieldTypeN CompMinMax
valuesCellsfloat6410.000e+001.000e+01

.. 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) .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_c_create-uniform-grid_001.png :alt: c create uniform grid :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_c_create-uniform-grid_001.png :class: sphx-glr-single-img .. 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 .. rst-class:: sphx-glr-script-out .. code-block:: none (20, 5, 10) .. 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 .. raw:: html
ImageDataInformation
N Cells684
N Points1000
X Bounds1.000e+02, 1.190e+02
Y Bounds3.300e+01, 5.300e+01
Z Bounds5.560e+01, 7.360e+01
Dimensions20, 5, 10
Spacing1.000e+00, 5.000e+00, 2.000e+00
N Arrays1
NameFieldTypeN CompMinMax
valuesPointsfloat6410.000e+001.000e+01