{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Fill rule demo\n\nBy default, patches such as `~.patches.Polygon` are filled according to the\n[non-zero winding fill rule](https://en.wikipedia.org/wiki/Nonzero-rule)_.\nAny given point has a winding number, which is the number of times the path\nwraps around the point in the clockwise direction. For this fill rule, the\nfilled regions are where the winding number is non-zero. See\n:doc:`/gallery/shapes_and_collections/donut` for an example of how to leverage\nthe winding directions in multiple segments of a path under this fill rule.\n\nThe other option for fill rule is the\n[even-odd fill rule](https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule)_,\nwhich is specified by setting the patch's ``fill_rule`` property to \"evenodd\".\nFor this fill rule, the filled regions are where the winding number is an odd\nnumber. This fill rule allows for the construction of patterns of fill\nregions that would otherwise take many more vertices to construct under the\nnon-zero winding fill rule.\n\nThis example demonstrates the difference between the two fill rules for a single\n`~.patches.Polygon` that intersects itself multiple times. The winding number\nfor each closed region is labeled.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.patches import Polygon\n\nfig, axs = plt.subplots(1, 2)\n\nvertices = np.array([[0, 0, 6, 6, 1, 1, 5, 5, 2, 2, 4, 4, 3, 3, 5, 5],\n [2, 5, 5, 0, 0, 7, 7, 3, 3, 4, 4, 6, 6, 1, 1, 2]]).T\n\nlabels = ['1', '0', '1', '2', '3', '2', '1', '1', '0']\nlabel_xys = np.array([[2.0, 4.0, 0.5, 1.5, 2.5, 4.0, 3.5, 2.0, 3.5],\n [1.5, 1.5, 3.5, 3.5, 3.5, 3.5, 4.5, 5.5, 5.5]]).T\n\nfor ax, fill_rule in zip(axs, ['nonzero', 'evenodd']):\n polygon = Polygon(vertices, facecolor='green', edgecolor='red',\n fill_rule=fill_rule)\n ax.add_patch(polygon)\n\n ax.plot(*vertices.T, '.', markersize=10, color='red')\n\n for label, label_xy in zip(labels, label_xys):\n ax.text(*label_xy, label, ha='center', va='center')\n\n ax.set_axis_off()\n ax.set_title(f'fill_rule={fill_rule}')\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.patches`\n - `matplotlib.patches.Polygon`\n - `matplotlib.axes.Axes.add_patch`\n - `matplotlib.patches.Patch.set_fill_rule`\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 }