{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Tricontour Smooth User\n\nDemonstrates high-resolution tricontouring on user-defined triangular grids\nwith `matplotlib.tri.UniformTriRefiner`.\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\n\n# ----------------------------------------------------------------------------\n# Analytical test function\n# ----------------------------------------------------------------------------\ndef function_z(x, y):\n r1 = np.sqrt((0.5 - x)**2 + (0.5 - y)**2)\n theta1 = np.arctan2(0.5 - x, 0.5 - y)\n r2 = np.sqrt((-x - 0.2)**2 + (-y - 0.2)**2)\n theta2 = np.arctan2(-x - 0.2, -y - 0.2)\n z = -(2 * (np.exp((r1 / 10)**2) - 1) * 30. * np.cos(7. * theta1) +\n (np.exp((r2 / 10)**2) - 1) * 30. * np.cos(11. * theta2) +\n 0.7 * (x**2 + y**2))\n return (np.max(z) - z) / (np.max(z) - np.min(z))\n\n# ----------------------------------------------------------------------------\n# Creating a Triangulation\n# ----------------------------------------------------------------------------\n# First create the x and y coordinates of the points.\nn_angles = 20\nn_radii = 10\nmin_radius = 0.15\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()\nz = function_z(x, y)\n\n# Now create the Triangulation.\n# (Creating a Triangulation without specifying the triangles results in the\n# Delaunay triangulation of the points.)\ntriang = tri.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\n# ----------------------------------------------------------------------------\nrefiner = tri.UniformTriRefiner(triang)\ntri_refi, z_test_refi = refiner.refine_field(z, subdiv=3)\n\n# ----------------------------------------------------------------------------\n# Plot the triangulation and the high-res iso-contours\n# ----------------------------------------------------------------------------\nfig, ax = plt.subplots()\nax.set_aspect('equal')\nax.triplot(triang, lw=0.5, color='white')\n\nlevels = np.arange(0., 1., 0.025)\nax.tricontourf(tri_refi, z_test_refi, levels=levels, cmap='terrain')\nax.tricontour(tri_refi, z_test_refi, levels=levels,\n colors=['0.25', '0.5', '0.5', '0.5', '0.5'],\n linewidths=[1.0, 0.5, 0.5, 0.5, 0.5])\n\nax.set_title(\"High-resolution tricontouring\")\n\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.tricontourf` / `matplotlib.pyplot.tricontourf`\n - `matplotlib.tri`\n - `matplotlib.tri.Triangulation`\n - `matplotlib.tri.UniformTriRefiner`\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 }