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

Skip to content

Commit 2daa442

Browse files
committed
DOC: improve hatch example
DOC: improve hatch example
1 parent 187e87b commit 2daa442

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

examples/shapes_and_collections/hatch_demo.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,50 @@
11
"""
2-
==========
3-
Hatch Demo
4-
==========
2+
=======
3+
Hatches
4+
=======
5+
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.
515
6-
Hatching (pattern filled polygons) is supported currently in the PS,
7-
PDF, SVG and Agg backends only.
816
"""
17+
918
import numpy as np
1019
import matplotlib.pyplot as plt
1120
from matplotlib.patches import Ellipse, Polygon
1221

22+
import matplotlib
23+
print(matplotlib.get_backend())
1324
x = np.arange(1, 5)
1425
y1 = np.arange(1, 5)
1526
y2 = np.ones(y1.shape) * 4
1627

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])
28+
fig = plt.figure()
29+
axs = fig.subplot_mosaic([['bar1', 'patches'], ['bar2', 'patches']])
2230

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])
31+
ax = axs['bar1']
32+
ax.bar(x, y1, edgecolor='black', hatch="/")
33+
ax.bar(x, y2, bottom=y1, edgecolor='black', hatch='//')
2634

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))
35+
ax = axs['bar2']
36+
ax.bar(x, y1, edgecolor='black', hatch=['--', '+', 'x', '\\'])
37+
ax.bar(x, y2, bottom=y1, edgecolor='black', hatch=['*', 'o', 'O', '.'])
3338

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

3650
#############################################################################

0 commit comments

Comments
 (0)