3
3
Anchored Artists
4
4
================
5
5
6
+ This example illustrates the use of the anchored objects without the
7
+ helper classes found in the AxesGrid toolbox. This version of the figure
8
+ is similar to the one found in examples/axes_grid1/simple_anchored_artists.py,
9
+ but it is implemented using only the matplotlib namespace, without the help
10
+ of additional toolkits.
6
11
"""
7
12
13
+ from matplotlib import pyplot as plt
8
14
from matplotlib .patches import Rectangle , Ellipse
9
15
from matplotlib .offsetbox import (
10
16
AnchoredOffsetbox , AuxTransformBox , DrawingArea , TextArea , VPacker )
@@ -18,27 +24,34 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5,
18
24
child = self .txt , prop = prop , frameon = frameon )
19
25
20
26
21
- class AnchoredSizeBar (AnchoredOffsetbox ):
22
- def __init__ (self , transform , size , label , loc ,
23
- pad = 0.1 , borderpad = 0.1 , sep = 2 , prop = None , frameon = True ):
24
- """
25
- Draw a horizontal bar with the size in data coordinate of the given
26
- axes. A label will be drawn underneath (center-aligned).
27
+ def draw_text (ax ):
28
+ """
29
+ Draw a text-box anchored to the upper-left corner of the figure.
30
+ """
31
+ # loc=2 is equivalent to loc='upper left'
32
+ at = AnchoredText ("Figure 1a" , loc = 2 , frameon = True )
33
+ at .patch .set_boxstyle ("round,pad=0.,rounding_size=0.2" )
34
+ ax .add_artist (at )
27
35
28
- pad, borderpad in fraction of the legend font size (or prop)
29
- sep in points.
30
- """
31
- self .size_bar = AuxTransformBox (transform )
32
- self .size_bar .add_artist (Rectangle ((0 , 0 ), size , 0 , ec = "black" , lw = 1.0 ))
33
36
34
- self .txt_label = TextArea (label , minimumdescent = False )
37
+ class AnchoredDrawingArea (AnchoredOffsetbox ):
38
+ def __init__ (self , width , height , xdescent , ydescent ,
39
+ loc , pad = 0.4 , borderpad = 0.5 , prop = None , frameon = True ):
40
+ self .da = DrawingArea (width , height , xdescent , ydescent )
41
+ super ().__init__ (loc , pad = pad , borderpad = borderpad ,
42
+ child = self .da , prop = None , frameon = frameon )
35
43
36
- self ._box = VPacker (children = [self .size_bar , self .txt_
10000
label ],
37
- align = "center" ,
38
- pad = 0 , sep = sep )
39
44
40
- super ().__init__ (loc , pad = pad , borderpad = borderpad ,
41
- child = self ._box , prop = prop , frameon = frameon )
45
+ def draw_circle (ax ):
46
+ """
47
+ Draw a circle in axis coordinates
48
+ """
49
+ from matplotlib .patches import Circle
50
+ ada = AnchoredDrawingArea (20 , 20 , 0 , 0 ,
51
+ loc = 1 , pad = 0. , frameon = False )
52
+ p = Circle ((10 , 10 ), 10 )
53
+ ada .da .add_artist (p )
54
+ ax .add_artist (ada )
42
55
43
56
44
57
class AnchoredEllipse (AnchoredOffsetbox ):
@@ -56,41 +69,44 @@ def __init__(self, transform, width, height, angle, loc,
56
69
child = self ._box , prop = prop , frameon = frameon )
57
70
58
71
59
- class AnchoredDrawingArea ( AnchoredOffsetbox ):
60
- def __init__ ( self , width , height , xdescent , ydescent ,
61
- loc , pad = 0.4 , borderpad = 0.5 , prop = None , frameon = True ):
62
- self . da = DrawingArea ( width , height , xdescent , ydescent )
63
- super (). __init__ ( loc , pad = pad , borderpad = borderpad ,
64
- child = self . da , prop = None , frameon = frameon )
72
+ def draw_ellipse ( ax ):
73
+ """
74
+ Draw an ellipse of width =0.1, height =0.15 in data coordinates
75
+ """
76
+ ae = AnchoredEllipse ( ax . transData , width = 0.1 , height = 0.15 , angle = 0. ,
77
+ loc = 3 , pad = 0.5 , borderpad = 0.4 , frameon = True )
65
78
79
+ ax .add_artist (ae )
66
80
67
- if __name__ == "__main__" :
68
81
69
- import matplotlib .pyplot as plt
82
+ class AnchoredSizeBar (AnchoredOffsetbox ):
83
+ def __init__ (self , transform , size , label , loc ,
84
+ pad = 0.1 , borderpad = 0.1 , sep = 2 , prop = None , frameon = True ):
85
+ """
86
+ Draw a horizontal bar with the size in data coordinate of the given
87
+ axes. A label will be drawn underneath (center-aligned).
70
88
71
- ax = plt .gca ()
72
- ax .set_aspect (1. )
89
+ pad, borderpad in fraction of the legend font size (or prop)
90
+ sep in points.
91
+ """
92
+ self .size_bar = AuxTransformBox (transform )
93
+ self .size_bar .add_artist (Rectangle ((0 , 0 ), size , 0 , ec = "black" , lw = 1.0 ))
73
94
74
- at = AnchoredText ("Figure 1a" ,
75
- loc = 2 , frameon = True )
76
- at .patch .set_boxstyle ("round,pad=0.,rounding_size=0.2" )
77
- ax .add_artist (at )
95
+ self .txt_label = TextArea (label , minimumdescent = False )
78
96
79
- from matplotlib .patches import Circle
80
- ada = AnchoredDrawingArea (20 , 20 , 0 , 0 ,
81
- loc = 1 , pad = 0. , frameon = False )
82
- p = Circle ((10 , 10 ), 10 )
83
- ada .da .add_artist (p )
84
- ax .add_artist (ada )
97
+ self ._box = VPacker (children = [self .size_bar , self .txt_label ],
98
+ align = "center" ,
99
+ pad = 0 , sep = sep )
85
100
86
- # draw an ellipse of width=0.1, height=0.15 in the data coordinate
87
- ae = AnchoredEllipse (ax .transData , width = 0.1 , height = 0.15 , angle = 0. ,
88
- loc = 3 , pad = 0.5 , borderpad = 0.4 , frameon = True )
101
+ super ().__init__ (loc , pad = pad , borderpad = borderpad ,
102
+ child = self ._box , prop = prop , frameon = frameon )
89
103
90
- ax .add_artist (ae )
91
104
92
- # draw a horizontal bar with length of 0.1 in Data coordinate
93
- # (ax.transData) with a label underneath.
105
+ def draw_sizebar (ax ):
106
+ """
107
+ Draw a horizontal bar with length of 0.1 in data coordinates,
108
+ with a fixed label underneath.
109
+ """
94
110
asb = AnchoredSizeBar (ax .transData ,
95
111
0.1 ,
96
112
r"1$^{\prime}$" ,
@@ -99,5 +115,13 @@ def __init__(self, width, height, xdescent, ydescent,
99
115
frameon = False )
100
116
ax .add_artist (asb )
101
117
102
- plt .draw ()
103
- plt .show ()
118
+
119
+ ax = plt .gca ()
120
+ ax .set_aspect (1. )
121
+
122
+ draw_text (ax )
123
+ draw_circle (ax )
124
+ draw_ellipse (ax )
125
+ draw_sizebar (ax )
126
+
127
+ plt .show ()
0 commit comments