{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Contour plot of irregularly spaced data\n\nComparison of a contour plot of irregularly spaced data interpolated\non a regular grid versus a tricontour plot for an unstructured triangular grid.\n\nSince `~.axes.Axes.contour` and `~.axes.Axes.contourf` expect the data to live\non a regular grid, plotting a contour plot of irregularly spaced data requires\ndifferent methods. The two options are:\n\n* Interpolate the data to a regular grid first. This can be done with on-board\n  means, e.g. via `~.tri.LinearTriInterpolator` or using external functionality\n  e.g. via `scipy.interpolate.griddata`. Then plot the interpolated data with\n  the usual `~.axes.Axes.contour`.\n* Directly use `~.axes.Axes.tricontour` or `~.axes.Axes.tricontourf` which will\n  perform a triangulation internally.\n\nThis example shows both methods in action.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.tri as tri\n\nnp.random.seed(19680801)\nnpts = 200\nngridx = 100\nngridy = 200\nx = np.random.uniform(-2, 2, npts)\ny = np.random.uniform(-2, 2, npts)\nz = x * np.exp(-x**2 - y**2)\n\nfig, (ax1, ax2) = plt.subplots(nrows=2)\n\n# -----------------------\n# Interpolation on a grid\n# -----------------------\n# A contour plot of irregularly spaced data coordinates\n# via interpolation on a grid.\n\n# Create grid values first.\nxi = np.linspace(-2.1, 2.1, ngridx)\nyi = np.linspace(-2.1, 2.1, ngridy)\n\n# Linearly interpolate the data (x, y) on a grid defined by (xi, yi).\ntriang = tri.Triangulation(x, y)\ninterpolator = tri.LinearTriInterpolator(triang, z)\nXi, Yi = np.meshgrid(xi, yi)\nzi = interpolator(Xi, Yi)\n\n# Note that scipy.interpolate provides means to interpolate data on a grid\n# as well. The following would be an alternative to the four lines above:\n# from scipy.interpolate import griddata\n# zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='linear')\n\nax1.contour(xi, yi, zi, levels=14, linewidths=0.5, colors='k')\ncntr1 = ax1.contourf(xi, yi, zi, levels=14, cmap=\"RdBu_r\")\n\nfig.colorbar(cntr1, ax=ax1)\nax1.plot(x, y, 'ko', ms=3)\nax1.set(xlim=(-2, 2), ylim=(-2, 2))\nax1.set_title('grid and contour (%d points, %d grid points)' %\n              (npts, ngridx * ngridy))\n\n# ----------\n# Tricontour\n# ----------\n# Directly supply the unordered, irregularly spaced coordinates\n# to tricontour.\n\nax2.tricontour(x, y, z, levels=14, linewidths=0.5, colors='k')\ncntr2 = ax2.tricontourf(x, y, z, levels=14, cmap=\"RdBu_r\")\n\nfig.colorbar(cntr2, ax=ax2)\nax2.plot(x, y, 'ko', ms=3)\nax2.set(xlim=(-2, 2), ylim=(-2, 2))\nax2.set_title('tricontour (%d points)' % npts)\n\nplt.subplots_adjust(hspace=0.5)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n   The use of the following functions, methods, classes and modules is shown\n   in this example:\n\n   - `matplotlib.axes.Axes.contour` / `matplotlib.pyplot.contour`\n   - `matplotlib.axes.Axes.contourf` / `matplotlib.pyplot.contourf`\n   - `matplotlib.axes.Axes.tricontour` / `matplotlib.pyplot.tricontour`\n   - `matplotlib.axes.Axes.tricontourf` / `matplotlib.pyplot.tricontourf`\n\n"
      ]
    }
  ],
  "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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}