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

Skip to content

Commit 660f931

Browse files
authored
Merge pull request #17929 from jklymak/doc-merged-hatch
Improve hatch demo
2 parents b560513 + a1b7862 commit 660f931

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

examples/shapes_and_collections/hatch_demo.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
"""
22
==========
3-
Hatch Demo
3+
Hatch demo
44
==========
55
6-
Hatching (pattern filled polygons) is supported currently in the PS,
7-
PDF, SVG and Agg backends only.
6+
Hatches can be added to most polygons in Matplotlib, including `~.Axes.bar`,
7+
`~.Axes.fill_between`, `~.Axes.contourf`, and childern of `~.patches.Polygon`.
8+
They are currently supported in the PS, PDF, SVG, OSX, and Agg backends. The WX
9+
and Cairo backends do not currently support hatching.
10+
11+
See also :doc:`/gallery/images_contours_and_fields/contourf_hatching` for
12+
an example using `~.Axes.contourf`, and
13+
:doc:`/gallery/shapes_and_collections/hatch_style_reference` for swatches
14+
of the existing hatches.
15+
816
"""
17+
918
import numpy as np
1019
import matplotlib.pyplot as plt
1120
from matplotlib.patches import Ellipse, Polygon
@@ -14,23 +23,26 @@
1423
y1 = np.arange(1, 5)
1524
y2 = np.ones(y1.shape) * 4
1625

17-
fig, (ax1, ax2, ax3) = plt.subplots(3)
18-
19-
ax1.bar(x, y1, edgecolor='black', hatch="/")
20-
ax1.bar(x, y2, bottom=y1, edgecolor='black', hatch='//')
21-
ax1.set_xticks([1.5, 2.5, 3.5, 4.5])
26+
fig = plt.figure()
27+
axs = fig.subplot_mosaic([['bar1', 'patches'], ['bar2', 'patches']])
2228

23-
ax2.bar(x, y1, edgecolor='black', hatch=['-', '+', 'x', '\\'])
24-
ax2.bar(x, y2, bottom=y1, edgecolor='black', hatch=['*', 'o', 'O', '.'])
25-
ax2.set_xticks([1.5, 2.5, 3.5, 4.5])
29+
axs['bar1'].bar(x, y1, edgecolor='black', hatch="/")
30+
axs['bar1'].bar(x, y2, bottom=y1, edgecolor='black', hatch='//')
2631

27-
ax3.fill([1, 3, 3, 1], [1, 1, 2, 2], fill=False, hatch='\\')
28-
ax3.add_patch(Ellipse((4, 1.5), 4, 0.5, fill=False, hatch='*'))
29-
ax3.add_patch(Polygon([[0, 0], [4, 1.1], [6, 2.5], [2, 1.4]], closed=True,
30-
fill=False, hatch='/'))
31-
ax3.set_xlim((0, 6))
32-
ax3.set_ylim((0, 2.5))
32+
axs['bar2'].bar(x, y1, edgecolor='black', hatch=['--', '+', 'x', '\\'])
33+
axs['bar2'].bar(x, y2, bottom=y1, edgecolor='black',
34+
hatch=['*', 'o', 'O', '.'])
3335

36+
x = np.arange(0, 40, 0.2)
37+
axs['patches'].fill_between(x, np.sin(x) * 4 + 30, y2=0,
38+
hatch='///', zorder=2, fc='c')
39+
axs['patches'].add_patch(Ellipse((4, 50), 10, 10, fill=True,
40+
hatch='*', facecolor='y'))
41+
axs['patches'].add_patch(Polygon([(10, 20), (30, 50), (50, 10)],
42+
hatch='\\/...', facecolor='g'))
43+
axs['patches'].set_xlim([0, 40])
44+
axs['patches'].set_ylim([10, 60])
45+
axs['patches'].set_aspect(1)
3446
plt.show()
3547

3648
#############################################################################

examples/shapes_and_collections/hatch_style_reference.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
Hatch style reference
44
=====================
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+
Hatches can be added to most polygons in Matplotlib, including `~.Axes.bar`,
7+
`~.Axes.fill_between`, `~.Axes.contourf`, and childern of `~.patches.Polygon`.
8+
They are currently supported in the PS, PDF, SVG, OSX, and Agg backends. The WX
9+
and Cairo backends do not currently support hatching.
10+
11+
See also :doc:`/gallery/images_contours_and_fields/contourf_hatching` for
12+
an example using `~.Axes.contourf`, and
13+
:doc:`/gallery/shapes_and_collections/hatch_demo` for more usage examples.
14+
815
"""
916
import matplotlib.pyplot as plt
1017
from matplotlib.patches import Rectangle

0 commit comments

Comments
 (0)