{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Line, Poly and RegularPoly Collection\n\nFor the first two subplots, we will use spirals. Their size will be set in\nplot units, not data units. Their positions will be set in data units by using\nthe *offsets* and *offset_transform* keyword arguments of the `.LineCollection`\nand `.PolyCollection`.\n\nThe third subplot will make regular polygons, with the same\ntype of scaling and positioning as in the first two.\n\nThe last subplot illustrates the use of ``offsets=(xo, yo)``,\nthat is, a single tuple instead of a list of tuples, to generate\nsuccessively offset curves, with the offset given in data\nunits. This behavior is available only for the LineCollection.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib import collections, transforms\n\nnverts = 50\nnpts = 100\n\n# Make some spirals\nr = np.arange(nverts)\ntheta = np.linspace(0, 2*np.pi, nverts)\nxx = r * np.sin(theta)\nyy = r * np.cos(theta)\nspiral = np.column_stack([xx, yy])\n\n# Fixing random state for reproducibility\nrs = np.random.RandomState(19680801)\n\n# Make some offsets\nxyo = rs.randn(npts, 2)\n\n# Make a list of colors from the default color cycle.\ncolors = plt.rcParams['axes.prop_cycle'].by_key()['color']\n\nfig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)\nfig.subplots_adjust(top=0.92, left=0.07, right=0.97,\n hspace=0.3, wspace=0.3)\n\n\ncol = collections.LineCollection(\n [spiral], offsets=xyo, offset_transform=ax1.transData, color=colors)\n# transform the line segments such that their size is given in points\ntrans = fig.dpi_scale_trans + transforms.Affine2D().scale(1.0/72.0)\ncol.set_transform(trans) # the points to pixels transform\nax1.add_collection(col)\nax1.set_title('LineCollection using offsets')\n\n\n# The same data as above, but fill the curves.\ncol = collections.PolyCollection(\n [spiral], offsets=xyo, offset_transform=ax2.transData, color=colors)\ntrans = transforms.Affine2D().scale(fig.dpi/72.0)\ncol.set_transform(trans) # the points to pixels transform\nax2.add_collection(col)\nax2.set_title('PolyCollection using offsets')\n\n\n# 7-sided regular polygons\ncol = collections.RegularPolyCollection(\n 7, sizes=np.abs(xx) * 10.0, offsets=xyo, offset_transform=ax3.transData,\n color=colors)\ntrans = transforms.Affine2D().scale(fig.dpi / 72.0)\ncol.set_transform(trans) # the points to pixels transform\nax3.add_collection(col)\nax3.set_title('RegularPolyCollection using offsets')\n\n\n# Simulate a series of ocean current profiles, successively\n# offset by 0.1 m/s so that they form what is sometimes called\n# a \"waterfall\" plot or a \"stagger\" plot.\nnverts = 60\nncurves = 20\noffs = (0.1, 0.0)\n\nyy = np.linspace(0, 2*np.pi, nverts)\nym = np.max(yy)\nxx = (0.2 + (ym - yy) / ym) ** 2 * np.cos(yy - 0.4) * 0.5\nsegs = []\nfor i in range(ncurves):\n xxx = xx + 0.02*rs.randn(nverts)\n curve = np.column_stack([xxx, yy * 100])\n segs.append(curve)\n\ncol = collections.LineCollection(segs, offsets=offs, color=colors)\nax4.add_collection(col)\nax4.set_title('Successive data offsets')\nax4.set_xlabel('Zonal velocity component (m/s)')\nax4.set_ylabel('Depth (m)')\nax4.invert_yaxis() # so that depth increases downward\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.figure.Figure`\n - `matplotlib.collections`\n - `matplotlib.collections.LineCollection`\n - `matplotlib.collections.RegularPolyCollection`\n - `matplotlib.axes.Axes.add_collection`\n - `matplotlib.transforms.Affine2D`\n - `matplotlib.transforms.Affine2D.scale`\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.12.15" } }, "nbformat": 4, "nbformat_minor": 0 }