{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "7a185f0e",
            "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": "45cc25f0",
            "cell_type": "markdown",
            "source": "Suppose that we want to bilinearly interpolate an exponentially decaying\nfunction in 2 dimensions.\n",
            "metadata": {}
        },
        {
            "id": "64a35fcb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.interpolate import RectBivariateSpline",
            "outputs": []
        },
        {
            "id": "d66dbc8f",
            "cell_type": "markdown",
            "source": "We sample the function on a coarse grid. Note that the default indexing=\"xy\"\nof meshgrid would result in an unexpected (transposed) result after\ninterpolation.\n",
            "metadata": {}
        },
        {
            "id": "ccf6de94",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "xarr = np.linspace(-3, 3, 100)\nyarr = np.linspace(-3, 3, 100)\nxgrid, ygrid = np.meshgrid(xarr, yarr, indexing=\"ij\")",
            "outputs": []
        },
        {
            "id": "7ff14ac0",
            "cell_type": "markdown",
            "source": "The function to interpolate decays faster along one axis than the other.\n",
            "metadata": {}
        },
        {
            "id": "6fe75b21",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "zdata = np.exp(-np.sqrt((xgrid / 2) ** 2 + ygrid**2))",
            "outputs": []
        },
        {
            "id": "bfc834c0",
            "cell_type": "markdown",
            "source": "Next we sample on a finer grid using interpolation (kx=ky=1 for bilinear).\n",
            "metadata": {}
        },
        {
            "id": "10fc5f77",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "rbs = RectBivariateSpline(xarr, yarr, zdata, kx=1, ky=1)\nxarr_fine = np.linspace(-3, 3, 200)\nyarr_fine = np.linspace(-3, 3, 200)\nxgrid_fine, ygrid_fine = np.meshgrid(xarr_fine, yarr_fine, indexing=\"ij\")\nzdata_interp = rbs(xgrid_fine, ygrid_fine, grid=False)",
            "outputs": []
        },
        {
            "id": "1a75fa98",
            "cell_type": "markdown",
            "source": "And check that the result agrees with the input by plotting both.\n",
            "metadata": {}
        },
        {
            "id": "c073a2cf",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nfig = plt.figure()\nax1 = fig.add_subplot(1, 2, 1, aspect=\"equal\")\nax2 = fig.add_subplot(1, 2, 2, aspect=\"equal\")\nax1.imshow(zdata)\nax2.imshow(zdata_interp)\nplt.show()",
            "outputs": []
        }
    ]
}