3
3
Reference for Matplotlib artists
4
4
================================
5
5
6
- This example displays several of Matplotlib's graphics primitives (artists)
7
- drawn using matplotlib API. A full list of artists and the documentation is
8
- available at :ref:`the artist API <artist-api>`.
6
+ This example displays several of Matplotlib's graphics primitives (artists).
7
+ A full list of artists is documented at :ref:`the artist API <artist-api>`.
8
+
9
+ See also :doc:`/gallery/shapes_and_collections/patch_collection`, which groups
10
+ all artists into a single `.PatchCollection` instead.
9
11
10
12
Copyright (c) 2010, Bartosz Telenczuk
11
13
BSD License
12
14
"""
13
- import matplotlib .pyplot as plt
14
- import numpy as np
15
- import matplotlib .path as mpath
15
+
16
+ import matplotlib as mpl
16
17
import matplotlib .lines as mlines
17
18
import matplotlib .patches as mpatches
18
- from matplotlib .collections import PatchCollection
19
-
20
-
21
- def label (xy , text ):
22
- y = xy [1 ] - 0.15 # shift y-value for label so that it's below the artist
23
- plt .text (xy [0 ], y , text , ha = "center" , family = 'sans-serif' , size = 14 )
24
-
25
-
26
- fig , ax = plt .subplots ()
27
- # create 3x3 grid to plot the artists
28
- grid = np .mgrid [0.2 :0.8 :3j , 0.2 :0.8 :3j ].reshape (2 , - 1 ).T
29
-
30
- patches = []
31
-
32
- # add a circle
33
- circle = mpatches .Circle (grid [0 ], 0.1 , ec = "none" )
34
- patches .append (circle )
35
- label (grid [0 ], "Circle" )
36
-
37
- # add a rectangle
38
- rect = mpatches .Rectangle (grid [1 ] - [0.025 , 0.05 ], 0.05 , 0.1 , ec = "none" )
39
- patches .append (rect )
40
- label (grid [1 ], "Rectangle" )
41
-
42
- # add a wedge
43
- wedge = mpatches .Wedge (grid [2 ], 0.1 , 30 , 270 , ec = "none" )
44
- patches .append (wedge )
45
- label (grid [2 ], "Wedge" )
46
-
47
- # add a Polygon
48
- polygon = mpatches .RegularPolygon (grid [3 ], 5 , radius = 0.1 )
49
- patches .append (polygon )
50
- label (grid [3 ], "Polygon" )
51
-
52
- # add an ellipse
53
- ellipse = mpatches .Ellipse (grid [4 ], 0.2 , 0.1 )
54
- patches .append (ellipse )
55
- label (grid [4 ], "Ellipse" )
56
-
57
- # add an arrow
58
- arrow = mpatches .Arrow (grid [5 , 0 ] - 0.05 , grid [5 , 1 ] - 0.05 , 0.1 , 0.1 ,
59
- width = 0.1 )
60
- patches .append (arrow )
61
- label (grid [5 ], "Arrow" )
19
+ import matplotlib .path as mpath
20
+ import matplotlib .pyplot as plt
62
21
63
- # add a path patch
22
+ # Prepare the data for the PathPatch below.
64
23
Path = mpath .Path
65
- path_data = [
24
+ codes , verts = zip ( * [
66
25
(Path .MOVETO , [0.018 , - 0.11 ]),
67
26
(Path .CURVE4 , [- 0.031 , - 0.051 ]),
68
27
(Path .CURVE4 , [- 0.115 , 0.073 ]),
@@ -71,35 +30,28 @@ def label(xy, text):
71
30
(Path .CURVE4 , [0.043 , 0.121 ]),
72
31
(Path .CURVE4 , [0.075 , - 0.005 ]),
73
32
(Path .CURVE4 , [0.035 , - 0.027 ]),
74
- (Path .CLOSEPOLY , [0.018 , - 0.11 ])]
75
- codes , verts = zip (* path_data )
76
- path = mpath .Path (verts + grid [6 ], codes )
77
- patch = mpatches .PathPatch (path )
78
- patches .append (patch )
79
- label (grid [6 ], "PathPatch" )
80
-
81
- # add a fancy box
82
- fancybox = mpatches .FancyBboxPatch (
83
- grid [7 ] - [0.025 , 0.05 ], 0.05 , 0.1 ,
84
- boxstyle = mpatches .BoxStyle ("Round" , pad = 0.02 ))
85
- patches .append (fancybox )
86
- label (grid [7 ], "FancyBboxPatch" )
87
-
88
- # add a line
89
- x , y = ([- 0.06 , 0.0 , 0.1 ], [0.05 , - 0.05 , 0.05 ])
90
- line = mlines .Line2D (x + grid [8 , 0 ], y + grid [8 , 1 ], lw = 5. , alpha = 0.3 )
91
- label (grid [8 ], "Line2D" )
92
-
93
- colors = np .linspace (0 , 1 , len (patches ))
94
- collection = PatchCollection (patches , cmap = plt .cm .hsv , alpha = 0.3 )
95
- collection .set_array (colors )
96
- ax .add_collection (collection )
97
- ax .add_line (line )
98
-
99
- plt .axis ('equal' )
100
- plt .axis ('off' )
101
- plt .tight_layout ()
102
-
33
+ (Path .CLOSEPOLY , [0.018 , - 0.11 ])])
34
+
35
+ artists = [
36
+ mpatches .Circle ((0 , 0 ), 0.1 , ec = "none" ),
37
+ mpatches .Rectangle ((- 0.025 , - 0.05 ), 0.05 , 0.1 , ec = "none" ),
38
+ mpatches .Wedge ((0 , 0 ), 0.1 , 30 , 270 , ec = "none" ),
39
+ mpatches .RegularPolygon ((0 , 0 ), 5 , radius = 0.1 ),
40
+ mpatches .Ellipse ((0 , 0 ), 0.2 , 0.1 ),
41
+ mpatches .Arrow (- 0.05 , - 0.05 , 0.1 , 0.1 , width = 0.1 ),
42
+ mpatches .PathPatch (mpath .Path (verts , codes ), ec = "none" ),
43
+ mpatches .FancyBboxPatch ((- 0.025 , - 0.05 ), 0.05 , 0.1 , ec = "none" ,
44
+ boxstyle = mpatches .BoxStyle ("Round" , pad = 0.02 )),
45
+ mlines .Line2D ([- 0.06 , 0.0 , 0.1 ], [0.05 , - 0.05 , 0.05 ], lw = 5 ),
46
+ ]
47
+
48
+ axs = plt .figure (figsize = (6 , 6 ), layout = "constrained" ).subplots (3 , 3 )
49
+ for i , (ax , artist ) in enumerate (zip (axs .flat , artists )):
50
+ artist .set (color = mpl .colormaps ["hsv" ](i / len (artists )))
51
+ ax .add_artist (artist )
52
+ ax .set (title = type (artist ).__name__ ,
53
+ aspect = 1 , xlim = (- .2 , .2 ), ylim = (- .2 , .2 ))
54
+ ax .set_axis_off ()
103
55
plt .show ()
104
56
105
57
# %%
@@ -122,8 +74,4 @@ def label(xy, text):
122
74
# - `matplotlib.patches.PathPatch`
123
75
# - `matplotlib.patches.FancyBboxPatch`
124
76
# - `matplotlib.patches.RegularPolygon`
125
- # - `matplotlib.collections`
126
- # - `matplotlib.collections.PatchCollection`
127
- # - `matplotlib.cm.ScalarMappable.set_array`
128
- # - `matplotlib.axes.Axes.add_collection`
129
- # - `matplotlib.axes.Axes.add_line`
77
+ # - `matplotlib.axes.Axes.add_artist`
0 commit comments