{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Triinterp Demo\n\nInterpolation from triangular grid to quad grid.\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 mtri\n\n# Create triangulation.\nx = np.asarray([0, 1, 2, 3, 0.5, 1.5, 2.5, 1, 2, 1.5])\ny = np.asarray([0, 0, 0, 0, 1.0, 1.0, 1.0, 2, 2, 3.0])\ntriangles = [[0, 1, 4], [1, 2, 5], [2, 3, 6], [1, 5, 4], [2, 6, 5], [4, 5, 7],\n [5, 6, 8], [5, 8, 7], [7, 8, 9]]\ntriang = mtri.Triangulation(x, y, triangles)\n\n# Interpolate to regularly-spaced quad grid.\nz = np.cos(1.5 * x) * np.cos(1.5 * y)\nxi, yi = np.meshgrid(np.linspace(0, 3, 20), np.linspace(0, 3, 20))\n\ninterp_lin = mtri.LinearTriInterpolator(triang, z)\nzi_lin = interp_lin(xi, yi)\n\ninterp_cubic_geom = mtri.CubicTriInterpolator(triang, z, kind='geom')\nzi_cubic_geom = interp_cubic_geom(xi, yi)\n\ninterp_cubic_min_E = mtri.CubicTriInterpolator(triang, z, kind='min_E')\nzi_cubic_min_E = interp_cubic_min_E(xi, yi)\n\n# Set up the figure\nfig, axs = plt.subplots(nrows=2, ncols=2)\naxs = axs.flatten()\n\n# Plot the triangulation.\naxs[0].tricontourf(triang, z)\naxs[0].triplot(triang, 'ko-')\naxs[0].set_title('Triangular grid')\n\n# Plot linear interpolation to quad grid.\naxs[1].contourf(xi, yi, zi_lin)\naxs[1].plot(xi, yi, 'k-', lw=0.5, alpha=0.5)\naxs[1].plot(xi.T, yi.T, 'k-', lw=0.5, alpha=0.5)\naxs[1].set_title(\"Linear interpolation\")\n\n# Plot cubic interpolation to quad grid, kind=geom\naxs[2].contourf(xi, yi, zi_cubic_geom)\naxs[2].plot(xi, yi, 'k-', lw=0.5, alpha=0.5)\naxs[2].plot(xi.T, yi.T, 'k-', lw=0.5, alpha=0.5)\naxs[2].set_title(\"Cubic interpolation,\\nkind='geom'\")\n\n# Plot cubic interpolation to quad grid, kind=min_E\naxs[3].contourf(xi, yi, zi_cubic_min_E)\naxs[3].plot(xi, yi, 'k-', lw=0.5, alpha=0.5)\naxs[3].plot(xi.T, yi.T, 'k-', lw=0.5, alpha=0.5)\naxs[3].set_title(\"Cubic interpolation,\\nkind='min_E'\")\n\nfig.tight_layout()\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.tricontourf` / `matplotlib.pyplot.tricontourf`\n - `matplotlib.axes.Axes.triplot` / `matplotlib.pyplot.triplot`\n - `matplotlib.axes.Axes.contourf` / `matplotlib.pyplot.contourf`\n - `matplotlib.axes.Axes.plot` / `matplotlib.pyplot.plot`\n - `matplotlib.tri`\n - `matplotlib.tri.LinearTriInterpolator`\n - `matplotlib.tri.CubicTriInterpolator`\n - `matplotlib.tri.Triangulation`\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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }