{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "4e14d9a8",
            "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": "fc24e248",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import stats\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D",
            "outputs": []
        },
        {
            "id": "965cb36a",
            "cell_type": "markdown",
            "source": "Take an array of 600 (x, y) coordinates as an example.\n`binned_statistic_dd` can handle arrays of higher dimension `D`. But a plot\nof dimension `D+1` is required.\n",
            "metadata": {}
        },
        {
            "id": "eaf5808d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "mu = np.array([0., 1.])\nsigma = np.array([[1., -0.5],[-0.5, 1.5]])\nmultinormal = stats.multivariate_normal(mu, sigma)\ndata = multinormal.rvs(size=600, random_state=235412)\ndata.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(600, 2)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e0427920",
            "cell_type": "markdown",
            "source": "Create bins and count how many arrays fall in each bin:\n",
            "metadata": {}
        },
        {
            "id": "96d9fbf9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "N = 60\nx = np.linspace(-3, 3, N)\ny = np.linspace(-3, 4, N)\nret = stats.binned_statistic_dd(data, np.arange(600), bins=[x, y],\n                                statistic='count')\nbincounts = ret.statistic",
            "outputs": []
        },
        {
            "id": "136ce9ea",
            "cell_type": "markdown",
            "source": "Set the volume and the location of bars:\n",
            "metadata": {}
        },
        {
            "id": "0f795125",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "dx = x[1] - x[0]\ndy = y[1] - y[0]\nx, y = np.meshgrid(x[:-1]+dx/2, y[:-1]+dy/2)\nz = 0",
            "outputs": []
        },
        {
            "id": "e022598f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "bincounts = bincounts.ravel()\nx = x.ravel()\ny = y.ravel()",
            "outputs": []
        },
        {
            "id": "2065719c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig = plt.figure()\nax = fig.add_subplot(111, projection='3d')\nwith np.errstate(divide='ignore'):   # silence random axes3d warning\n    ax.bar3d(x, y, z, dx, dy, bincounts)",
            "outputs": []
        },
        {
            "id": "e1ed8179",
            "cell_type": "markdown",
            "source": "Reuse bin numbers and bin edges with new values:\n",
            "metadata": {}
        },
        {
            "id": "7a6a1c84",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ret2 = stats.binned_statistic_dd(data, -np.arange(600),\n                                 binned_statistic_result=ret,\n                                 statistic='mean')",
            "outputs": []
        }
    ]
}