{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Trigradient Demo\n\nDemonstrates computation of gradient with\n`matplotlib.tri.CubicTriInterpolator`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.tri import (CubicTriInterpolator, Triangulation,\n UniformTriRefiner)\n\n\n# ----------------------------------------------------------------------------\n# Electrical potential of a dipole\n# ----------------------------------------------------------------------------\ndef dipole_potential(x, y):\n \"\"\"The electric dipole potential V, at position *x*, *y*.\"\"\"\n r_sq = x**2 + y**2\n theta = np.arctan2(y, x)\n z = np.cos(theta)/r_sq\n return (np.max(z) - z) / (np.max(z) - np.min(z))\n\n\n# ----------------------------------------------------------------------------\n# Creating a Triangulation\n# ----------------------------------------------------------------------------\n# First create the x and y coordinates of the points.\nn_angles = 30\nn_radii = 10\nmin_radius = 0.2\nradii = np.linspace(min_radius, 0.95, n_radii)\n\nangles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)\nangles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)\nangles[:, 1::2] += np.pi / n_angles\n\nx = (radii*np.cos(angles)).flatten()\ny = (radii*np.sin(angles)).flatten()\nV = dipole_potential(x, y)\n\n# Create the Triangulation; no triangles specified so Delaunay triangulation\n# created.\ntriang = Triangulation(x, y)\n\n# Mask off unwanted triangles.\ntriang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),\n y[triang.triangles].mean(axis=1))\n < min_radius)\n\n# ----------------------------------------------------------------------------\n# Refine data - interpolates the electrical potential V\n# ----------------------------------------------------------------------------\nrefiner = UniformTriRefiner(triang)\ntri_refi, z_test_refi = refiner.refine_field(V, subdiv=3)\n\n# ----------------------------------------------------------------------------\n# Computes the electrical field (Ex, Ey) as gradient of electrical potential\n# ----------------------------------------------------------------------------\ntci = CubicTriInterpolator(triang, -V)\n# Gradient requested here at the mesh nodes but could be anywhere else:\n(Ex, Ey) = tci.gradient(triang.x, triang.y)\nE_norm = np.sqrt(Ex**2 + Ey**2)\n\n# ----------------------------------------------------------------------------\n# Plot the triangulation, the potential iso-contours and the vector field\n# ----------------------------------------------------------------------------\nfig, ax = plt.subplots()\nax.set_aspect('equal')\n# Enforce the margins, and enlarge them to give room for the vectors.\nax.use_sticky_edges = False\nax.margins(0.07)\n\nax.triplot(triang, color='0.8')\n\nlevels = np.arange(0., 1., 0.01)\nax.tricontour(tri_refi, z_test_refi, levels=levels, cmap='hot',\n linewidths=[2.0, 1.0, 1.0, 1.0])\n# Plots direction of the electrical vector field\nax.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,\n units='xy', scale=10., zorder=3, color='blue',\n width=0.007, headwidth=3., headlength=4.)\n\nax.set_title('Gradient plot: an electrical dipole')\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.tricontour` / `matplotlib.pyplot.tricontour`\n - `matplotlib.axes.Axes.triplot` / `matplotlib.pyplot.triplot`\n - `matplotlib.tri`\n - `matplotlib.tri.Triangulation`\n - `matplotlib.tri.CubicTriInterpolator`\n - `matplotlib.tri.CubicTriInterpolator.gradient`\n - `matplotlib.tri.UniformTriRefiner`\n - `matplotlib.axes.Axes.quiver` / `matplotlib.pyplot.quiver`\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 }