diff --git a/galleries/examples/shapes_and_collections/hatch_style_reference.py b/galleries/examples/shapes_and_collections/hatch_style_reference.py index 58f3207cb9d8..6815fbd6ff77 100644 --- a/galleries/examples/shapes_and_collections/hatch_style_reference.py +++ b/galleries/examples/shapes_and_collections/hatch_style_reference.py @@ -35,10 +35,16 @@ def hatches_plot(ax, h): # %% # Hatching patterns can be repeated to increase the density. +# +# .. note:: +# In regular (non-raw) Python strings, backslashes must be escaped: +# ``'\\\\'`` and ``r'\\'`` are both the two-character hatch ``\\``. +# Forgetting this silently halves the density — ``'\\\\\\'`` is only a +# triple hatch and thus sparser than ``'//////'``. fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2)) -hatches = ['//', '\\\\', '||', '--', '++', 'xx', 'oo', 'OO', '..', '**'] +hatches = ['//', r'\\', '||', '--', '++', 'xx', 'oo', 'OO', '..', '**'] for ax, h in zip(axs.flat, hatches): hatches_plot(ax, h) @@ -48,7 +54,7 @@ def hatches_plot(ax, h): fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2)) -hatches = ['/o', '\\|', '|*', '-\\', '+o', 'x*', 'o-', 'O|', 'O.', '*-'] +hatches = ['/o', r'\|', '|*', r'\-', '+o', 'x*', 'o-', 'O|', 'O.', '*-'] for ax, h in zip(axs.flat, hatches): hatches_plot(ax, h) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index cdd03bcd7e89..711927b556ca 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -637,6 +637,9 @@ def set_hatch(self, hatch): hatchings are done. If same letter repeats, it increases the density of hatching of that pattern. + In regular (non-raw) Python strings, backslashes must be doubled: + ``'\\\\'`` and ``r'\\'`` are both a double back-diagonal hatch. + Parameters ---------- hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}