{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {},
  "_ordered_sections": [],
  "item_file": null,
  "item_line": null,
  "item_type": null,
  "aliases": [],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": null,
  "references": null,
  "qa": "tutorial:interpolate:ND_unstructured",
  "arbitrary": [
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "tutorial-interpolate_NDunstructured"
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Suppose you have multidimensional data, for instance, for an underlying function "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "f(x, y)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " you only know the values at points "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "(x[i], y[i])"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " that do not form a regular grid."
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "Suppose we want to interpolate the 2-D function\n\n>>> import numpy as np\n>>> def func(x, y):\n...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2\n\non a grid in [0, 1]x[0, 1]\n\n>>> grid_x, grid_y = np.meshgrid(np.linspace(0, 1, 100),\n...                              np.linspace(0, 1, 200), indexing='ij')\n\nbut we only know its values at 1000 data points:\n\n>>> rng = np.random.default_rng()\n>>> points = rng.random((1000, 2))\n>>> values = func(points[:,0], points[:,1])\n\nThis can be done with `griddata` -- below, we try out all of the\ninterpolation methods:\n\n>>> from scipy.interpolate import griddata\n>>> grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')\n>>> grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')\n>>> grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')\n\nOne can see that the exact result is reproduced by all of the\nmethods to some degree, but for this smooth function the piecewise\ncubic interpolant gives the best results (black dots show the data being\ninterpolated):\n\n>>> import matplotlib.pyplot as plt\n>>> plt.subplot(221)\n>>> plt.imshow(func(grid_x, grid_y).T, extent=(0, 1, 0, 1), origin='lower')\n>>> plt.plot(points[:, 0], points[:, 1], 'k.', ms=1)   # data\n>>> plt.title('Original')\n>>> plt.subplot(222)\n>>> plt.imshow(grid_z0.T, extent=(0, 1, 0, 1), origin='lower')\n>>> plt.title('Nearest')\n>>> plt.subplot(223)\n>>> plt.imshow(grid_z1.T, extent=(0, 1, 0, 1), origin='lower')\n>>> plt.title('Linear')\n>>> plt.subplot(224)\n>>> plt.imshow(grid_z2.T, extent=(0, 1, 0, 1), origin='lower')\n>>> plt.title('Cubic')\n>>> plt.gcf().set_size_inches(6, 6)\n>>> plt.show()",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "For each interpolation method, this function delegates to a corresponding class object --- these classes can be used directly as well --- "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "NearestNDInterpolator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "LinearNDInterpolator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "CloughTocher2DInterpolator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "                                                      for piecewise cubic interpolation in 2D."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "All these interpolation methods rely on triangulation of the data using the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "QHull"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " library wrapped in "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "scipy.spatial",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "*",
                "kind": "api",
                "path": "scipy.spatial"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Admonition",
          "__tag": 4056,
          "kind": "note",
          "base_type": "note",
          "children": [
            {
              "__type": "AdmonitionTitle",
              "__tag": 4055,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "note "
                }
              ]
            },
            {
              "__type": "Paragraph",
              "__tag": 4045,
              "children": [
                {
                  "__type": "InlineRole",
                  "__tag": 4003,
                  "value": "griddata",
                  "domain": null,
                  "role": null,
                  "inventory": null
                },
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": " is based on triangulation, hence is appropriate for unstructured, scattered data. If your data is on a full grid,  the "
                },
                {
                  "__type": "InlineRole",
                  "__tag": 4003,
                  "value": "griddata",
                  "domain": null,
                  "role": null,
                  "inventory": null
                },
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": " function --- despite its name --- is not the right tool. Use "
                },
                {
                  "__type": "InlineRole",
                  "__tag": 4003,
                  "value": "RegularGridInterpolator",
                  "domain": null,
                  "role": null,
                  "inventory": null
                },
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": " instead."
                }
              ]
            }
          ]
        },
        {
          "__type": "Admonition",
          "__tag": 4056,
          "kind": "note",
          "base_type": "note",
          "children": [
            {
              "__type": "AdmonitionTitle",
              "__tag": 4055,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "note "
                }
              ]
            },
            {
              "__type": "Paragraph",
              "__tag": 4045,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "If the input data is such that input dimensions have incommensurate units and differ by many orders of magnitude, the interpolant may have numerical artifacts. Consider rescaling the data before interpolating or use the "
                },
                {
                  "__type": "InlineCode",
                  "__tag": 4051,
                  "value": "rescale=True"
                },
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": " keyword argument to "
                },
                {
                  "__type": "InlineRole",
                  "__tag": 4003,
                  "value": "griddata",
                  "domain": null,
                  "role": null,
                  "inventory": null
                },
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "."
                }
              ]
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Scattered data interpolation ("
        },
        {
          "__type": "InlineRole",
          "__tag": 4003,
          "value": "griddata",
          "domain": null,
          "role": "func",
          "inventory": null
        },
        {
          "__type": "Text",
          "__tag": 4046,
          "value": ")"
        }
      ],
      "level": 0,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Radial basis functions can be used for smoothing/interpolating scattered data in N dimensions, but should be used with caution for extrapolation outside of the observed data range."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Using radial basis functions for smoothing/interpolation"
        }
      ],
      "level": 0,
      "target": "tutorial-interpolate_RBF"
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This example compares the usage of the "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "RBFInterpolator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "UnivariateSpline",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " classes from the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "scipy.interpolate",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "*",
                "kind": "api",
                "path": "scipy.interpolate"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " module."
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> import numpy as np\n>>> from scipy.interpolate import RBFInterpolator, InterpolatedUnivariateSpline\n>>> import matplotlib.pyplot as plt\n\n>>> # setup data\n>>> x = np.linspace(0, 10, 9).reshape(-1, 1)\n>>> y = np.sin(x)\n>>> xi = np.linspace(0, 10, 101).reshape(-1, 1)\n\n>>> # use fitpack2 method\n>>> ius = InterpolatedUnivariateSpline(x, y)\n>>> yi = ius(xi)\n\n>>> fix, (ax1, ax2) = plt.subplots(2, 1)\n>>> ax1.plot(x, y, 'bo')\n>>> ax1.plot(xi, yi, 'g')\n>>> ax1.plot(xi, np.sin(xi), 'r')\n>>> ax1.set_title('Interpolation using univariate spline')\n\n>>> # use RBF method\n>>> rbf = RBFInterpolator(x, y)\n>>> fi = rbf(xi)\n\n>>> ax2.plot(x, y, 'bo')\n>>> ax2.plot(xi, fi, 'g')\n>>> ax2.plot(xi, np.sin(xi), 'r')\n>>> ax2.set_title('Interpolation using RBF - multiquadrics')\n>>> plt.tight_layout()\n>>> plt.show()",
          "execution_status": null
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "1-D Example"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This example shows how to interpolate scattered 2-D data:"
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> import numpy as np\n>>> from scipy.interpolate import RBFInterpolator\n>>> import matplotlib.pyplot as plt\n\n>>> # 2-d tests - setup scattered data\n>>> rng = np.random.default_rng()\n>>> xy = rng.random((100, 2))*4.0-2.0\n>>> z = xy[:, 0]*np.exp(-xy[:, 0]**2-xy[:, 1]**2)\n>>> edges = np.linspace(-2.0, 2.0, 101)\n>>> centers = edges[:-1] + np.diff(edges[:2])[0] / 2.\n>>> x_i, y_i = np.meshgrid(centers, centers)\n>>> x_i = x_i.reshape(-1, 1)\n>>> y_i = y_i.reshape(-1, 1)\n>>> xy_i = np.concatenate([x_i, y_i], axis=1)\n\n>>> # use RBF\n>>> rbf = RBFInterpolator(xy, z, epsilon=2)\n>>> z_i = rbf(xy_i)\n\n>>> # plot the result\n>>> fig, ax = plt.subplots()\n>>> X_edges, Y_edges = np.meshgrid(edges, edges)\n>>> lims = dict(cmap='RdBu_r', vmin=-0.4, vmax=0.4)\n>>> mapping = ax.pcolormesh(\n...     X_edges, Y_edges, z_i.reshape(100, 100),\n...     shading='flat', **lims\n... )\n>>> ax.scatter(xy[:, 0], xy[:, 1], 100, z, edgecolor='w', lw=0.1, **lims)\n>>> ax.set(\n...     title='RBF interpolation - multiquadrics',\n...     xlim=(-2, 2),\n...     ylim=(-2, 2),\n... )\n>>> fig.colorbar(mapping)",
          "execution_status": null
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "2-D Example"
        }
      ],
      "level": 1,
      "target": null
    }
  ],
  "local_refs": []
}