33Anchored Artists
44================
55
6+ This example illustrates the use of the anchored objects without the
7+ helper classes found in the :ref:`toolkit_axesgrid1-index`. This version
8+ of the figure is similar to the one found in
9+ :ref:`sphx_glr_gallery_axes_grid1_simple_anchored_artists.py`, but it is
10+ implemented using only the matplotlib namespace, without the help
11+ of additional toolkits.
612"""
713
14+ from matplotlib import pyplot as plt
815from matplotlib .patches import Rectangle , Ellipse
916from matplotlib .offsetbox import (
1017 AnchoredOffsetbox , AuxTransformBox , DrawingArea , TextArea , VPacker )
@@ -18,27 +25,34 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5,
1825 child = self .txt , prop = prop , frameon = frameon )
1926
2027
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).
28+ def draw_text (ax ):
29+ """
30+ Draw a text-box anchored to the upper-left corner of the figure.
31+ """
32+ # loc=2 is equivalent to loc='upper left'
33+ at = AnchoredText ("Figure 1a" , loc = 2 , frameon = True )
34+ at .patch .set_boxstyle ("round,pad=0.,rounding_size=0.2" )
35+ ax .add_artist (at )
2736
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 ))
3337
34- self .txt_label = TextArea (label , minimumdescent = False )
38+ class AnchoredDrawingArea (AnchoredOffsetbox ):
39+ def __init__ (self , width , height , xdescent , ydescent ,
40+ loc , pad = 0.4 , borderpad = 0.5 , prop = None , frameon = True ):
41+ self .da = DrawingArea (width , height , xdescent , ydescent )
42+ super ().__init__ (loc , pad = pad , borderpad = borderpad ,
43+ child = self .da , prop = None , frameon = frameon )
3544
36- self ._box = VPacker (children = [self .size_bar , self .txt_label ],
37- align = "center" ,
38- pad = 0 , sep = sep )
3945
40- super ().__init__ (loc , pad = pad , borderpad = borderpad ,
41- child = self ._box , prop = prop , frameon = frameon )
46+ def draw_circle (ax ):
47+ """
48+ Draw a circle in axis coordinates
49+ """
50+ from matplotlib .patches import Circle
51+ ada = AnchoredDrawingArea (20 , 20 , 0 , 0 ,
52+ loc = 1 , pad = 0. , frameon = False )
53+ p = Circle ((10 , 10 ), 10 )
54+ ada .da .add_artist (p )
55+ ax .add_artist (ada )
4256
4357
4458class AnchoredEllipse (AnchoredOffsetbox ):
@@ -56,41 +70,44 @@ def __init__(self, transform, width, height, angle, loc,
5670 child = self ._box , prop = prop , frameon = frameon )
5771
5872
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 )
73+ def draw_ellipse ( ax ):
74+ """
75+ Draw an ellipse of width =0.1, height =0.15 in data coordinates
76+ """
77+ ae = AnchoredEllipse ( ax . transData , width = 0.1 , height = 0.15 , angle = 0. ,
78+ loc = 3 , pad = 0.5 , borderpad = 0.4 , frameon = True )
6579
80+ ax .add_artist (ae )
6681
67- if __name__ == "__main__" :
6882
69- import matplotlib .pyplot as plt
83+ class AnchoredSizeBar (AnchoredOffsetbox ):
84+ def __init__ (self , transform , size , label , loc ,
85+ pad = 0.1 , borderpad = 0.1 , sep = 2 , prop = None , frameon = True ):
86+ """
87+ Draw a horizontal bar with the size in data coordinate of the given
88+ axes. A label will be drawn underneath (center-aligned).
7089
71- ax = plt .gca ()
72- ax .set_aspect (1. )
90+ pad, borderpad in fraction of the legend font size (or prop)
91+ sep in points.
92+ """
93+ self .size_bar = AuxTransformBox (transform )
94+ self .size_bar .add_artist (Rectangle ((0 , 0 ), size , 0 , ec = "black" , lw = 1.0 ))
7395
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 )
96+ self .txt_label = TextArea (label , minimumdescent = False )
7897
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 )
98+ self ._box = VPacker (children = [self .size_bar , self .txt_label ],
99+ align = "center" ,
100+ pad = 0 , sep = sep )
85101
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 )
102+ super ().__init__ (loc , pad = pad , borderpad = borderpad ,
103+ child = self ._box , prop = prop , frameon = frameon )
89104
90- ax .add_artist (ae )
91105
92- # draw a horizontal bar with length of 0.1 in Data coordinate
93- # (ax.transData) with a label underneath.
106+ def draw_sizebar (ax ):
107+ """
108+ Draw a horizontal bar with length of 0.1 in data coordinates,
109+ with a fixed label underneath.
110+ """
94111 asb = AnchoredSizeBar (ax .transData ,
95112 0.1 ,
96113 r"1$^{\prime}$" ,
@@ -99,5 +116,13 @@ def __init__(self, width, height, xdescent, ydescent,
99116 frameon = False )
100117 ax .add_artist (asb )
101118
102- plt .draw ()
103- plt .show ()
119+
120+ ax = plt .gca ()
121+ ax .set_aspect (1. )
122+
123+ draw_text (ax )
124+ draw_circle (ax )
125+ draw_ellipse (ax )
126+ draw_sizebar (ax )
127+
128+ plt .show ()
0 commit comments