{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating a Uniform Grid\n\nCreate a simple uniform grid from a 3D NumPy array of values. This\nexample uses `pyvista.ImageData`{.interpreted-text role=\"class\"}.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nimport pyvista as pv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take a 3D NumPy array of data values that holds some spatial data where\neach axis corresponds to the XYZ cartesian axes. This example will\ncreate a `pyvista.ImageData`{.interpreted-text role=\"class\"} object that\nwill hold the spatial reference for a 3D grid which a 3D NumPy array of\nvalues can be plotted against.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the 3D NumPy array of spatially referenced data. This is\nspatially referenced such that the grid is 20 by 5 by 10 (nx by ny by\nnz)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "values = np.linspace(0, 10, 1000).reshape((20, 5, 10))\nvalues.shape\n\n# Create the spatial reference\ngrid = pv.ImageData()\n\n# Set the grid dimensions: shape + 1 because we want to inject our values on\n# the CELL data\ngrid.dimensions = np.array(values.shape) + 1\n\n# Edit the spatial reference\ngrid.origin = (100, 33, 55.6) # The bottom left corner of the data set\ngrid.spacing = (1, 5, 2) # These are the cell sizes along each axis\n\n# Add the data values to the cell data\ngrid.cell_data[\"values\"] = values.flatten(order=\"F\") # Flatten the array\n\n# Now plot the grid\ngrid.plot(show_edges=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Don\\'t like cell data? You could also add the NumPy array to the point\ndata of a `pyvista.ImageData`{.interpreted-text role=\"class\"}. Take note\nof the subtle difference when setting the grid dimensions upon\ninitialization.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Create the 3D NumPy array of spatially referenced data\n# This is spatially referenced such that the grid is 20 by 5 by 10\n# (nx by ny by nz)\nvalues = np.linspace(0, 10, 1000).reshape((20, 5, 10))\nvalues.shape\n\n# Create the spatial reference\ngrid = pv.ImageData()\n\n# Set the grid dimensions: shape because we want to inject our values on the\n# POINT data\ngrid.dimensions = values.shape\n\n# Edit the spatial reference\ngrid.origin = (100, 33, 55.6) # The bottom left corner of the data set\ngrid.spacing = (1, 5, 2) # These are the cell sizes along each axis\n\n# Add the data values to the cell data\ngrid.point_data[\"values\"] = values.flatten(order=\"F\") # Flatten the array\n\n# Now plot the grid\ngrid.plot(show_edges=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }