From 2988c3dccf4460a152abb412f5533d67f7b2e2e9 Mon Sep 17 00:00:00 2001 From: Andrzej Novak Date: Mon, 20 Jul 2026 13:51:17 +0200 Subject: [PATCH 1/2] DOC: Clarify backslash escaping for hatch patterns In non-raw Python strings each pair of backslashes yields a single hatch character, so e.g. '\\\\\\' is a triple, not sextuple, back-diagonal hatch. This silently halves the density and can look like '/' and '\' hatches are treated differently. Note this in Patch.set_hatch and in the hatch style reference example, and use raw strings in the example so the displayed code matches the figure labels. --- .../shapes_and_collections/hatch_style_reference.py | 10 ++++++++-- lib/matplotlib/patches.py | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/galleries/examples/shapes_and_collections/hatch_style_reference.py b/galleries/examples/shapes_and_collections/hatch_style_reference.py index 58f3207cb9d8..a5ef363ead89 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 doubled: +# ``'\\\\'`` 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..0ad9fdc8d09a 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -637,6 +637,10 @@ 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. + Forgetting this silently halves the density. + Parameters ---------- hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'} From b9d9f735b658831a1a9ceff2e1b8ba35ea04521a Mon Sep 17 00:00:00 2001 From: Andrzej Novak Date: Tue, 21 Jul 2026 08:37:44 +0200 Subject: [PATCH 2/2] fix: Apply suggestions from code review Co-authored-by: hannah --- .../examples/shapes_and_collections/hatch_style_reference.py | 2 +- lib/matplotlib/patches.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/galleries/examples/shapes_and_collections/hatch_style_reference.py b/galleries/examples/shapes_and_collections/hatch_style_reference.py index a5ef363ead89..6815fbd6ff77 100644 --- a/galleries/examples/shapes_and_collections/hatch_style_reference.py +++ b/galleries/examples/shapes_and_collections/hatch_style_reference.py @@ -37,7 +37,7 @@ def hatches_plot(ax, h): # Hatching patterns can be repeated to increase the density. # # .. note:: -# In regular (non-raw) Python strings, backslashes must be doubled: +# 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 ``'//////'``. diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 0ad9fdc8d09a..711927b556ca 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -639,7 +639,6 @@ def set_hatch(self, hatch): In regular (non-raw) Python strings, backslashes must be doubled: ``'\\\\'`` and ``r'\\'`` are both a double back-diagonal hatch. - Forgetting this silently halves the density. Parameters ----------