{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Hatchcolor Demo\n\nThe color of the hatch can be set using the *hatchcolor* parameter. The following\nexamples show how to use the *hatchcolor* parameter to set the color of the hatch\nin `~.patches.Patch` and `~.collections.Collection`.\n\nSee also :doc:`/gallery/shapes_and_collections/hatch_demo` for more usage examples\nof hatching.\n\n## Patch Hatchcolor\n\nThis example shows how to use the *hatchcolor* parameter to set the color of\nthe hatch in a rectangle and a bar plot. The *hatchcolor* parameter is available for\n`~.patches.Patch`, child classes of Patch, and methods that pass through to Patch.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.cm as cm\nfrom matplotlib.patches import Rectangle\n\nfig, (ax1, ax2) = plt.subplots(1, 2)\n\n# Rectangle with red hatch color and black edge color\nax1.add_patch(Rectangle((0.1, 0.5), 0.8, 0.3, hatch=\".\", hatchcolor='red',\n edgecolor='black', lw=2))\n# If hatchcolor is not passed, the hatch will match the edge color\nax1.add_patch(Rectangle((0.1, 0.1), 0.8, 0.3, hatch='x', edgecolor='orange', lw=2))\n\nx = np.arange(1, 5)\ny = np.arange(1, 5)\n\nax2.bar(x, y, facecolor='none', edgecolor='red', hatch='//', hatchcolor='blue')\nax2.set_xlim(0, 5)\nax2.set_ylim(0, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Collection Hatchcolor\n\nThe following example shows how to use the *hatchcolor* parameter to set the color of\nthe hatch in a scatter plot. The *hatchcolor* parameter can also be passed to\n`~.collections.Collection`, child classes of Collection, and methods that pass\nthrough to Collection.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nnum_points_x = 10\nnum_points_y = 9\nx = np.linspace(0, 1, num_points_x)\ny = np.linspace(0, 1, num_points_y)\n\nX, Y = np.meshgrid(x, y)\nX[1::2, :] += (x[1] - x[0]) / 2 # stagger every alternate row\n\n# As ax.scatter (PathCollection) is drawn row by row, setting hatchcolors to the\n# first row is enough, as the colors will be cycled through for the next rows.\ncolors = [cm.rainbow(val) for val in x]\n\nax.scatter(\n X.ravel(),\n Y.ravel(),\n s=1700,\n facecolor=\"none\",\n edgecolor=\"gray\",\n linewidth=2,\n marker=\"h\", # Use hexagon as marker\n hatch=\"xxx\",\n hatchcolor=colors,\n)\nax.set_xlim(0, 1)\nax.set_ylim(0, 1)\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.axes.Axes.bar` / `matplotlib.pyplot.bar`\n - `matplotlib.collections`\n - `matplotlib.axes.Axes.scatter` / `matplotlib.pyplot.scatter`\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 }