Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cd0a74

Browse files
committed
DOC: update hatch_style_reference
1 parent 4bcc1b1 commit 9cd0a74

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed
Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,66 @@
11
"""
2-
=====================
3-
Hatch style reference
4-
=====================
2+
=======
3+
Hatches
4+
=======
55
6-
Hatching (pattern filled polygons) is currently supported in the backends
7-
PS, PDF, SVG and Agg. The backends OSX, WX and Cairo ignore hatching.
6+
Hatching (pattern filled polygons) is currently supported in the PS, PDF, SVG,
7+
OSX, and Agg backends. The WX and Cairo backends ignore hatching.
8+
9+
See also :doc:`/gallery/images_contours_and_fields/contourf_hatching` for
10+
an example using `~.Axes.contourf`, and
11+
:doc:`/gallery/shapes_and_collections/hatch_demo` for examples using
12+
`~.Axes.bar` and polygons.
813
"""
14+
15+
import numpy as np
916
import matplotlib.pyplot as plt
10-
from matplotlib.patches import Rectangle
17+
from matplotlib.patches import Ellipse, Polygon, Rectangle
1118

12-
fig, axs = plt.subplots(2, 5, constrained_layout=True, figsize=(6.4, 3.2))
19+
x = np.arange(1, 5)
20+
y1 = np.arange(1, 5)
21+
y2 = np.ones(y1.shape) * 4
1322

14-
hatches = ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']
23+
fig, (ax1, ax2, ax3) = plt.subplots(3)
1524

25+
ax1.bar(x, y1, edgecolor='black', hatch="/")
26+
ax1.bar(x, y2, bottom=y1, edgecolor='black', hatch='//')
27+
ax1.set_xticks([1.5, 2.5, 3.5, 4.5])
1628

17-
def hatches_plot(ax, h):
18-
ax.add_patch(Rectangle((0, 0), 2, 2, fill=False, hatch=h))
19-
ax.text(1, -0.5, f"' {h} '", size=15, ha="center")
20-
ax.axis('equal')
21-
ax.axis('off')
22-
23-
for ax, h in zip(axs.flat, hatches):
24-
hatches_plot(ax, h)
29+
ax2.bar(x, y1, edgecolor='black', hatch=['-', '+', 'x', '\\'])
30+
ax2.bar(x, y2, bottom=y1, edgecolor='black', hatch=['*', 'o', 'O', '.'])
31+
ax2.set_xticks([1.5, 2.5, 3.5, 4.5])
2532

26-
###############################################################################
27-
# Hatching patterns can be repeated to increase the density.
33+
ax3.fill([1, 3, 3, 1], [1, 1, 2, 2], fill=False, hatch='\\')
34+
ax3.add_patch(Ellipse((4, 1.5), 4, 0.5, fill=False, hatch='*'))
35+
ax3.add_patch(Polygon([[0, 0], [4, 1.1], [6, 2.5], [2, 1.4]], closed=True,
36+
fill=False, hatch='/'))
37+
ax3.set_xlim((0, 6))
38+
ax3.set_ylim((0, 2.5))
2839

29-
fig, axs = plt.subplots(2, 5, constrained_layout=True, figsize=(6.4, 3.2))
40+
plt.show()
3041

31-
hatches = ['//', '\\\\', '||', '--', '++', 'xx', 'oo', 'OO', '..', '**']
42+
#############################################################################
43+
# Hatch style reference
44+
# ---------------------
45+
#
46+
# For convenience we document the 10 different possible hatch styles using the
47+
# code below:
3248

33-
for ax, h in zip(axs.flat, hatches):
34-
hatches_plot(ax, h)
49+
fig, axs = plt.subplots(2, 5, constrained_layout=True, figsize=(6, 3))
3550

36-
###############################################################################
37-
# Hatching patterns can be combined to create additional patterns.
51+
hatches = ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']
3852

39-
fig, axs = plt.subplots(2, 5, constrained_layout=True, figsize=(6.4, 3.2))
4053

41-
hatches = ['/o', '\\|', '|*', '-\\', '+o', 'x*', 'o-', 'O|', 'O.', '*-']
54+
def hatches_plot(ax, h):
55+
ax.add_patch(Rectangle((0, 0), 2, 2, fill=False, hatch=h))
56+
ax.text(1, -0.5, f"' {h} '", size=10, ha="center")
57+
ax.axis('equal')
58+
ax.axis('off')
4259

4360
for ax, h in zip(axs.flat, hatches):
4461
hatches_plot(ax, h)
62+
plt.show()
63+
4564

4665
#############################################################################
4766
#
@@ -55,6 +74,9 @@ def hatches_plot(ax, h):
5574

5675
import matplotlib
5776
matplotlib.patches
58-
matplotlib.patches.Rectangle
77+
matplotlib.patches.Ellipse
78+
matplotlib.patches.Polygon
5979
matplotlib.axes.Axes.add_patch
60-
matplotlib.axes.Axes.text
80+
matplotlib.patches.Patch.set_hatch
81+
matplotlib.axes.Axes.bar
82+
matplotlib.pyplot.bar

0 commit comments

Comments
 (0)