{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# More triangular 3D surfaces\n\nTwo additional examples of plotting surfaces with triangular mesh.\n\nThe first demonstrates use of plot_trisurf's triangles argument, and the\nsecond sets a `.Triangulation` object's mask and passes the object directly\nto plot_trisurf.\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\nfig = plt.figure(figsize=plt.figaspect(0.5))\n\n# ==========\n# First plot\n# ==========\n\n# Make a mesh in the space of parameterisation variables u and v\nu = np.linspace(0, 2.0 * np.pi, endpoint=True, num=50)\nv = np.linspace(-0.5, 0.5, endpoint=True, num=10)\nu, v = np.meshgrid(u, v)\nu, v = u.flatten(), v.flatten()\n\n# This is the Mobius mapping, taking a u, v pair and returning an x, y, z\n# triple\nx = (1 + 0.5 * v * np.cos(u / 2.0)) * np.cos(u)\ny = (1 + 0.5 * v * np.cos(u / 2.0)) * np.sin(u)\nz = 0.5 * v * np.sin(u / 2.0)\n\n# Triangulate parameter space to determine the triangles\ntri = mtri.Triangulation(u, v)\n\n# Plot the surface. The triangles in parameter space determine which x, y, z\n# points are connected by an edge.\nax = fig.add_subplot(1, 2, 1, projection='3d')\nax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap=plt.cm.Spectral)\nax.set_zlim(-1, 1)\n\n\n# ===========\n# Second plot\n# ===========\n\n# Make parameter spaces radii and angles.\nn_angles = 36\nn_radii = 8\nmin_radius = 0.25\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\n# Map radius, angle pairs to x, y, z points.\nx = (radii*np.cos(angles)).flatten()\ny = (radii*np.sin(angles)).flatten()\nz = (np.cos(radii)*np.cos(3*angles)).flatten()\n\n# Create the Triangulation; no triangles so Delaunay triangulation created.\ntriang = mtri.Triangulation(x, y)\n\n# Mask off unwanted triangles.\nxmid = x[triang.triangles].mean(axis=1)\nymid = y[triang.triangles].mean(axis=1)\nmask = xmid**2 + ymid**2 < min_radius**2\ntriang.set_mask(mask)\n\n# Plot the surface.\nax = fig.add_subplot(1, 2, 2, projection='3d')\nax.plot_trisurf(triang, z, cmap=plt.cm.CMRmap)\n\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n plot-type: 3D, plot-type: specialty,\n level: intermediate\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 }