@@ -46,8 +46,6 @@ def draw(self, renderer=None):
4646
4747if __name__ == "__main__" :
4848
49- usetex = plt .rcParams ["text.usetex" ]
50-
5149 fig , (ax1 , ax2 ) = plt .subplots (2 )
5250
5351 # EXAMPLE 1
@@ -68,30 +66,28 @@ def draw(self, renderer=None):
6866 ax1 .add_artist (ao )
6967
7068 # another text
71- from matplotlib .patches import PathPatch
72- if usetex :
73- r = r"\mbox{textpath supports mathtext \& \TeX}"
74- else :
75- r = r"textpath supports mathtext & TeX"
76-
77- text_path = TextPath ((0 , 0 ), r , size = 20 , usetex = usetex )
78-
79- p1 = PathPatch (text_path , ec = "w" , lw = 3 , fc = "w" , alpha = 0.9 ,
80- transform = IdentityTransform ())
81- p2 = PathPatch (text_path , ec = "none" , fc = "k" ,
82- transform = IdentityTransform ())
83-
84- offsetbox2 = AuxTransformBox (IdentityTransform ())
85- offsetbox2 .add_artist (p1 )
86- offsetbox2 .add_artist (p2 )
87-
88- ab = AnnotationBbox (offsetbox2 , (0.95 , 0.05 ),
89- xycoords = 'axes fraction' ,
90- boxcoords = "offset points" ,
91- box_alignment = (1. , 0. ),
92- frameon = False
93- )
94- ax1 .add_artist (ab )
69+ for usetex , ypos , string in [
70+ (False , 0.25 , r"textpath supports mathtext" ),
71+ (True , 0.05 , r"textpath supports \TeX" ),
72+ ]:
73+ text_path = TextPath ((0 , 0 ), string , size = 20 , usetex = usetex )
74+
75+ p1 = PathPatch (text_path , ec = "w" , lw = 3 , fc = "w" , alpha = 0.9 ,
76+ transform = IdentityTransform ())
77+ p2 = PathPatch (text_path , ec = "none" , fc = "k" ,
78+ transform = IdentityTransform ())
79+
80+ offsetbox2 = AuxTransformBox (IdentityTransform ())
81+ offsetbox2 .add_artist (p1 )
82+ offsetbox2 .add_artist (p2 )
83+
84+ ab = AnnotationBbox (offsetbox2 , (0.95 , ypos ),
85+ xycoords = 'axes fraction' ,
86+ boxcoords = "offset points" ,
87+ box_alignment = (1. , 0. ),
88+ frameon = False ,
89+ )
90+ ax1 .add_artist (ab )
9591
9692 ax1 .imshow ([[0 , 1 , 2 ], [1 , 2 , 3 ]], cmap = plt .cm .gist_gray_r ,
9793 interpolation = "bilinear" , aspect = "auto" )
@@ -100,32 +96,34 @@ def draw(self, renderer=None):
10096
10197 arr = np .arange (256 ).reshape (1 , 256 ) / 256
10298
103- if usetex :
104- s = (r"$\displaystyle\left[\sum_{n=1}^\infty"
105- r"\frac{-e^{i\pi}}{2^n}\right]$!" )
106- else :
107- s = r"$\left[\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}\right]$!"
108- text_path = TextPath ((0 , 0 ), s , size = 40 , usetex = usetex )
109- text_patch = PathClippedImagePatch (text_path , arr , ec = "none" ,
110- transform = IdentityTransform ())
111-
112- shadow1 = Shadow (text_patch , 1 , - 1 , fc = "none" , ec = "0.6" , lw = 3 )
113- shadow2 = Shadow (text_patch , 1 , - 1 , fc = "0.3" , ec = "none" )
114-
115- # make offset box
116- offsetbox = AuxTransformBox (IdentityTransform ())
117- offsetbox .add_artist (shadow1 )
118- offsetbox .add_artist (shadow2 )
119- offsetbox .add_artist (text_patch )
120-
121- # place the anchored offset box using AnnotationBbox
122- ab = AnnotationBbox (offsetbox , (0.5 , 0.5 ),
123- xycoords = 'data' ,
124- boxcoords = "offset points" ,
125- box_alignment = (0.5 , 0.5 ),
126- )
127-
128- ax2 .add_artist (ab )
99+ for usetex , xpos , string in [
100+ (False , 0.25 ,
101+ r"$\left[\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}\right]$!" ),
102+ (True , 0.75 ,
103+ r"$\displaystyle\left[\sum_{n=1}^\infty"
104+ r"\frac{-e^{i\pi}}{2^n}\right]$!" ),
105+ ]:
106+ text_path = TextPath ((0 , 0 ), string , size = 40 , usetex = usetex )
107+ text_patch = PathClippedImagePatch (text_path , arr , ec = "none" ,
108+ transform = IdentityTransform ())
109+
110+ shadow1 = Shadow (text_patch , 1 , - 1 , fc = "none" , ec = "0.6" , lw = 3 )
111+ shadow2 = Shadow (text_patch , 1 , - 1 , fc = "0.3" , ec = "none" )
112+
113+ # make offset box
114+ offsetbox = AuxTransformBox (IdentityTransform ())
115+ offsetbox .add_artist (shadow1 )
116+ offsetbox .add_artist (shadow2 )
117+ offsetbox .add_artist (text_patch )
118+
119+ # place the anchored offset box using AnnotationBbox
120+ ab = AnnotationBbox (offsetbox , (xpos , 0.5 ),
121+ xycoords = 'data' ,
122+ boxcoords = "offset points" ,
123+ box_alignment = (0.5 , 0.5 ),
124+ )
125+
126+ ax2 .add_artist (ab )
129127
130128 ax2 .set_xlim (0 , 1 )
131129 ax2 .set_ylim (0 , 1 )
0 commit comments