{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Hatch demo\n\nHatches can be added to most polygons in Matplotlib, including `~.Axes.bar`,\n`~.Axes.fill_between`, `~.Axes.contourf`, and children of `~.patches.Polygon`.\nThey are currently supported in the PS, PDF, SVG, macosx, and Agg backends. The WX\nand Cairo backends do not currently support hatching.\n\nSee also :doc:`/gallery/images_contours_and_fields/contourf_hatching` for\nan example using `~.Axes.contourf`, and\n:doc:`/gallery/shapes_and_collections/hatch_style_reference` for swatches\nof the existing hatches.\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 Ellipse, Polygon\n\nx = np.arange(1, 5)\ny1 = np.arange(1, 5)\ny2 = np.ones(y1.shape) * 4\n\nfig = plt.figure()\naxs = fig.subplot_mosaic([['bar1', 'patches'], ['bar2', 'patches']])\n\naxs['bar1'].bar(x, y1, edgecolor='black', hatch=\"/\")\naxs['bar1'].bar(x, y2, bottom=y1, edgecolor='black', hatch='//')\n\naxs['bar2'].bar(x, y1, edgecolor='black', hatch=['--', '+', 'x', '\\\\'])\naxs['bar2'].bar(x, y2, bottom=y1, edgecolor='black',\n hatch=['*', 'o', 'O', '.'])\n\nx = np.arange(0, 40, 0.2)\naxs['patches'].fill_between(x, np.sin(x) * 4 + 30, y2=0,\n hatch='///', zorder=2, fc='c')\naxs['patches'].add_patch(Ellipse((4, 50), 10, 10, fill=True,\n hatch='*', facecolor='y'))\naxs['patches'].add_patch(Polygon([(10, 20), (30, 50), (50, 10)],\n hatch='\\\\/...', facecolor='g'))\naxs['patches'].set_xlim([0, 40])\naxs['patches'].set_ylim([10, 60])\naxs['patches'].set_aspect(1)\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.Ellipse`\n - `matplotlib.patches.Polygon`\n - `matplotlib.axes.Axes.add_patch`\n - `matplotlib.patches.Patch.set_hatch`\n - `matplotlib.axes.Axes.bar` / `matplotlib.pyplot.bar`\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 }