{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Mmh Donuts!!!\n\nDraw donuts (miam!) using `~.path.Path`\\s and `~.patches.PathPatch`\\es.\nThis example shows the effect of the path's orientations in a compound path.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.patches as mpatches\nimport matplotlib.path as mpath\n\n\ndef wise(v):\n if v == 1:\n return \"CCW\"\n else:\n return \"CW\"\n\n\ndef make_circle(r):\n t = np.arange(0, np.pi * 2.0, 0.01)\n t = t.reshape((len(t), 1))\n x = r * np.cos(t)\n y = r * np.sin(t)\n return np.hstack((x, y))\n\nPath = mpath.Path\n\nfig, ax = plt.subplots()\n\ninside_vertices = make_circle(0.5)\noutside_vertices = make_circle(1.0)\ncodes = np.ones(\n len(inside_vertices), dtype=mpath.Path.code_type) * mpath.Path.LINETO\ncodes[0] = mpath.Path.MOVETO\n\nfor i, (inside, outside) in enumerate(((1, 1), (1, -1), (-1, 1), (-1, -1))):\n # Concatenate the inside and outside subpaths together, changing their\n # order as needed\n vertices = np.concatenate((outside_vertices[::outside],\n inside_vertices[::inside]))\n # Shift the path\n vertices[:, 0] += i * 2.5\n # The codes will be all \"LINETO\" commands, except for \"MOVETO\"s at the\n # beginning of each subpath\n all_codes = np.concatenate((codes, codes))\n # Create the Path object\n path = mpath.Path(vertices, all_codes)\n # Add plot it\n patch = mpatches.PathPatch(path, facecolor='#885500', edgecolor='black')\n ax.add_patch(patch)\n\n ax.annotate(f\"Outside {wise(outside)},\\nInside {wise(inside)}\",\n (i * 2.5, -1.5), va=\"top\", ha=\"center\")\n\nax.set_xlim(-2, 10)\nax.set_ylim(-3, 2)\nax.set_title('Mmm, donuts!')\nax.set_aspect(1.0)\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.path`\n - `matplotlib.path.Path`\n - `matplotlib.patches`\n - `matplotlib.patches.PathPatch`\n - `matplotlib.patches.Circle`\n - `matplotlib.axes.Axes.add_patch`\n - `matplotlib.axes.Axes.annotate`\n - `matplotlib.axes.Axes.set_aspect`\n - `matplotlib.axes.Axes.set_xlim`\n - `matplotlib.axes.Axes.set_ylim`\n - `matplotlib.axes.Axes.set_title`\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 }