DOC: Clarify backslash escaping for hatch patterns#32078
Conversation
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.
There was a problem hiding this comment.
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_hatchdocstring 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.', '*-'] |
There was a problem hiding this comment.
@copilot this is sth that should be determined by the test suite, you're just guessing...
| # 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
left a comment
There was a problem hiding this comment.
Thanks for catching this, made this mistake in a paper draft very recently.
Co-authored-by: hannah <story645@gmail.com>
|
I would generally recommend to use raw strings instead of doubling, because that's more readable. This affects all places, i.e. |
PR summary
Small clarification in the docs to explain why "////" and "\\\\" don't render a hatch of the same density.
AI Disclosure
Yup
PR checklist