{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "707845d2",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "4a650118",
            "cell_type": "markdown",
            "source": "Suppose we want to interpolate the 2-D function\n",
            "metadata": {}
        },
        {
            "id": "f647ad48",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\ndef func(x, y):\n    return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2",
            "outputs": []
        },
        {
            "id": "8ca5b34e",
            "cell_type": "markdown",
            "source": "on a grid in [0, 1]x[0, 1]\n",
            "metadata": {}
        },
        {
            "id": "27ca20b8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]",
            "outputs": []
        },
        {
            "id": "3e3a4358",
            "cell_type": "markdown",
            "source": "but we only know its values at 1000 data points:\n",
            "metadata": {}
        },
        {
            "id": "2a77386b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "rng = np.random.default_rng()\npoints = rng.random((1000, 2))\nvalues = func(points[:,0], points[:,1])",
            "outputs": []
        },
        {
            "id": "f4f41c61",
            "cell_type": "markdown",
            "source": "This can be done with `griddata` -- below we try out all of the\ninterpolation methods:\n",
            "metadata": {}
        },
        {
            "id": "cdd94490",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.interpolate import griddata\ngrid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')\ngrid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')\ngrid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')",
            "outputs": []
        },
        {
            "id": "67446456",
            "cell_type": "markdown",
            "source": "One 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:\n",
            "metadata": {}
        },
        {
            "id": "44d3d4ea",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nplt.subplot(221)\nplt.imshow(func(grid_x, grid_y).T, extent=(0,1,0,1), origin='lower')\nplt.plot(points[:,0], points[:,1], 'k.', ms=1)\nplt.title('Original')\nplt.subplot(222)\nplt.imshow(grid_z0.T, extent=(0,1,0,1), origin='lower')\nplt.title('Nearest')\nplt.subplot(223)\nplt.imshow(grid_z1.T, extent=(0,1,0,1), origin='lower')\nplt.title('Linear')\nplt.subplot(224)\nplt.imshow(grid_z2.T, extent=(0,1,0,1), origin='lower')\nplt.title('Cubic')\nplt.gcf().set_size_inches(6, 6)\nplt.show()",
            "outputs": []
        }
    ]
}