Hatch style reference

Hatch style reference#

Hatches can be added to most polygons in Matplotlib, including ~.Axes.bar, ~.Axes.fill_between, ~.Axes.contourf, and children of ~.patches.Polygon. They are currently supported in the PS, PDF, SVG, macosx, and Agg backends. The WX and Cairo backends do not currently support hatching.

See also :doc:/gallery/images_contours_and_fields/contourf_hatching for an example using ~.Axes.contourf, and :doc:/gallery/shapes_and_collections/hatch_demo for more usage examples.

import matplotlib.pyplot as plt

from matplotlib.patches import Rectangle

fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2))

hatches = ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']


def hatches_plot(ax, h):
    ax.add_patch(Rectangle((0, 0), 2, 2, fill=False, hatch=h))
    ax.text(1, -0.5, f"' {h} '", size=15, ha="center")
    ax.axis('equal')
    ax.axis('off')

for ax, h in zip(axs.flat, hatches):
    hatches_plot(ax, h)
../../../_images/911862eb3e4999bbbcddf39228541039360dea35f2e982a99ae67f5d1eb7d7b3.png

Hatching patterns can be repeated to increase the density.

fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2))

hatches = ['//', '\\\\', '||', '--', '++', 'xx', 'oo', 'OO', '..', '**']

for ax, h in zip(axs.flat, hatches):
    hatches_plot(ax, h)
../../../_images/ebe5d753bc9cd746c08f71d0bbc68c8ff3ad6870c97a65cf59bf7426602c6850.png

Hatching patterns can be combined to create additional patterns.

fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2))

hatches = ['/o', '\\|', '|*', '-\\', '+o', 'x*', 'o-', 'O|', 'O.', '*-']

for ax, h in zip(axs.flat, hatches):
    hatches_plot(ax, h)
../../../_images/93899f21f77eb3d4a834758330488dfc7ac4705378b5a6fa3894994c37d7e2ae.png

… admonition:: References

The use of the following functions, methods, classes and modules is shown in this example:

  • matplotlib.patches

  • matplotlib.patches.Rectangle

  • matplotlib.axes.Axes.add_patch

  • matplotlib.axes.Axes.text