1
1
"""
2
- =====================
3
- Hatch style reference
4
- =====================
2
+ =======
3
+ Hatches
4
+ =======
5
5
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.
8
13
"""
14
+
15
+ import numpy as np
9
16
import matplotlib .pyplot as plt
10
- from matplotlib .patches import Rectangle
17
+ from matplotlib .patches import Ellipse , Polygon , Rectangle
11
18
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
13
22
14
- hatches = [ '/' , ' \\ ' , '|' , '-' , '+' , 'x' , 'o' , 'O' , '.' , '*' ]
23
+ fig , ( ax1 , ax2 , ax3 ) = plt . subplots ( 3 )
15
24
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 ])
16
28
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 ])
25
32
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 ))
28
39
29
- fig , axs = plt .subplots ( 2 , 5 , constrained_layout = True , figsize = ( 6.4 , 3.2 ) )
40
+ plt .show ( )
30
41
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:
32
48
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 ))
35
50
36
- ###############################################################################
37
- # Hatching patterns can be combined to create additional patterns.
51
+ hatches = ['/' , '\\ ' , '|' , '-' , '+' , 'x' , 'o' , 'O' , '.' , '*' ]
38
52
39
- fig , axs = plt .subplots (2 , 5 , constrained_layout = True , figsize = (6.4 , 3.2 ))
40
53
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' )
42
59
43
60
for ax , h in zip (axs .flat , hatches ):
44
61
hatches_plot (ax , h )
62
+ plt .show ()
63
+
45
64
46
65
#############################################################################
47
66
#
@@ -55,6 +74,9 @@ def hatches_plot(ax, h):
55
74
56
75
import matplotlib
57
76
matplotlib .patches
58
- matplotlib .patches .Rectangle
77
+ matplotlib .patches .Ellipse
78
+ matplotlib .patches .Polygon
59
79
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