Skip to content

Commit f968c77

Browse files
andrzejnovakmeeseeksmachine
authored andcommitted
Backport PR matplotlib#32078: DOC: Clarify backslash escaping for hatch patterns
1 parent a2fcca5 commit f968c77

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

galleries/examples/shapes_and_collections/hatch_style_reference.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ def hatches_plot(ax, h):
3535

3636
# %%
3737
# Hatching patterns can be repeated to increase the density.
38+
#
39+
# .. note::
40+
# In regular (non-raw) Python strings, backslashes must be escaped:
41+
# ``'\\\\'`` and ``r'\\'`` are both the two-character hatch ``\\``.
42+
# Forgetting this silently halves the density — ``'\\\\\\'`` is only a
43+
# triple hatch and thus sparser than ``'//////'``.
3844

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

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

4349
for ax, h in zip(axs.flat, hatches):
4450
hatches_plot(ax, h)
@@ -48,7 +54,7 @@ def hatches_plot(ax, h):
4854

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

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

5359
for ax, h in zip(axs.flat, hatches):
5460
hatches_plot(ax, h)

lib/matplotlib/patches.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,9 @@ def set_hatch(self, hatch):
637637
hatchings are done. If same letter repeats, it increases the
638638
density of hatching of that pattern.
639639
640+
In regular (non-raw) Python strings, backslashes must be doubled:
641+
``'\\\\'`` and ``r'\\'`` are both a double back-diagonal hatch.
642+
640643
Parameters
641644
----------
642645
hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}

0 commit comments

Comments
 (0)