Skip to content

DOC: Clarify backslash escaping for hatch patterns#32078

Open
andrzejnovak wants to merge 2 commits into
matplotlib:mainfrom
andrzejnovak:doc-hatch-backslash
Open

DOC: Clarify backslash escaping for hatch patterns#32078
andrzejnovak wants to merge 2 commits into
matplotlib:mainfrom
andrzejnovak:doc-hatch-backslash

Conversation

@andrzejnovak

Copy link
Copy Markdown
Contributor

PR summary

Small clarification in the docs to explain why "////" and "\\\\" don't render a hatch of the same density.

  • The fact that it's escape characters is super obvious in retrospect, but not necessarily when one is focused on tweaking plot visuals :)

AI Disclosure

Yup

PR checklist

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.
Copilot AI review requested due to automatic review settings July 20, 2026 12:00
@github-actions github-actions Bot added the Documentation: examples files in galleries/examples label Jul 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR clarifies in the documentation how Python string escaping affects hatch density for backslash-based hatch patterns, to help users avoid accidentally producing sparser hatches than intended.

Changes:

  • Add an explanatory note to Patch.set_hatch docstring about doubling backslashes in non-raw Python strings.
  • Add a similar note to the hatch style reference gallery example and switch some hatch strings to raw-string forms for clarity.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/matplotlib/patches.py Adds docstring guidance on backslash escaping for hatch density.
galleries/examples/shapes_and_collections/hatch_style_reference.py Adds a Sphinx note about escaping and adjusts hatch string literals in the example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.', '*-']

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot this is sth that should be determined by the test suite, you're just guessing...

Comment on lines +40 to +43
# 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 ``'//////'``.

@story645 story645 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this, made this mistake in a paper draft very recently.

Comment thread galleries/examples/shapes_and_collections/hatch_style_reference.py Outdated
Comment thread lib/matplotlib/patches.py Outdated
Co-authored-by: hannah <story645@gmail.com>
@timhoffm

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation: examples files in galleries/examples

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants