Skip to content

[Doc] Added images of hatches to hatch API page #29644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions doc/_embedded_plots/hatch_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig, ax = plt.subplots()

pattern_to_class = {
'/': 'NorthEastHatch',
'\\': 'SouthEastHatch',
'|': 'VerticalHatch',
'-': 'HorizontalHatch',
'+': 'VerticalHatch + HorizontalHatch',
'x': 'NorthEastHatch + SouthEastHatch',
'o': 'SmallCircles',
'O': 'LargeCircles',
'.': 'SmallFilledCircles',
'*': 'Stars',
}

for i, (hatch, classes) in enumerate(pattern_to_class.items()):
r = Rectangle((0.1, i+0.5), 0.8, 0.8, fill=False, hatch=hatch*2)
ax.add_patch(r)
h = ax.annotate(f"'{hatch}'", xy=(1.2, .5), xycoords=r,
family='monospace', va='center', ha='left')
ax.annotate(pattern_to_class[hatch], xy=(1.5, .5), xycoords=h,
family='monospace', va='center', ha='left', color='tab:blue')

ax.set(xlim=(0, 5), ylim=(0, i+1.5), yinverted=True)
ax.set_axis_off()
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
.. _hatch_def:

=====================
Hatch style reference
=====================
Expand Down
19 changes: 18 additions & 1 deletion lib/matplotlib/hatch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
"""Contains classes for generating hatch patterns."""
"""
Module for generating hatch patterns.

For examples of using the hatch API, see
:ref:`sphx_glr_gallery_shapes_and_collections_hatch_style_reference.py`.

The following hatching patterns are available, shown here at level 2 density:

.. plot:: _embedded_plots/hatch_classes.py
:include-source: false
:alt:
8 squares, each showing the pattern corresponding to the hatch symbol:
symbol '/' makes right leaning diagonals, '\\' makes left leaning diagonals,
'|' makes vertical lines, '-' makes horizontal lines, '+' makes a grid,
'X' makes a grid rotated 90 degrees, 'o' makes small unfilled circles,
'O' makes large unfilled circles, '.' makes small filled circles, and '*' makes
a star with 5 points
"""

import numpy as np

Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/sphinxext/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def _mpltype_role(name, rawtext, text, lineno, inliner, options=None, content=No
mpltype = text
type_to_link_target = {
'color': 'colors_def',
'hatch': 'hatch_def',
}
if mpltype not in type_to_link_target:
raise ValueError(f"Unknown mpltype: {mpltype!r}")
Expand Down
Loading