|
1 | 1 | """
|
2 | 2 | ==========
|
3 |
| -Hatch Demo |
| 3 | +Hatch demo |
4 | 4 | ==========
|
5 | 5 |
|
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 | +
|
8 | 16 | """
|
| 17 | + |
9 | 18 | import numpy as np
|
10 | 19 | import matplotlib.pyplot as plt
|
11 | 20 | from matplotlib.patches import Ellipse, Polygon
|
|
14 | 23 | y1 = np.arange(1, 5)
|
15 | 24 | y2 = np.ones(y1.shape) * 4
|
16 | 25 |
|
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']]) |
22 | 28 |
|
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='//') |
26 | 31 |
|
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', '.']) |
33 | 35 |
|
| 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) |
34 | 46 | plt.show()
|
35 | 47 |
|
36 | 48 | #############################################################################
|
|
0 commit comments