@@ -12,36 +12,7 @@ def draw_text(data, obj):
12
12
properties = []
13
13
style = []
14
14
if isinstance (obj , mpl .text .Annotation ):
15
- ann_xy = obj .xy
16
- ann_xycoords = obj .xycoords
17
- ann_xytext = obj .xyann
18
- ann_textcoords = obj .anncoords
19
- if ann_xycoords != "data" or ann_textcoords != "data" :
20
- print (
21
- "Warning: Anything else except for explicit positioning "
22
- "is not supported for annotations yet :("
23
- )
24
- return data , content
25
- else : # Create a basic tikz arrow
26
- arrow_style = []
27
- if obj .arrowprops is not None :
28
- if obj .arrowprops ["arrowstyle" ] is not None :
29
- if obj .arrowprops ["arrowstyle" ] in ["-" , "->" , "<-" , "<->" ]:
30
- arrow_style .append (obj .arrowprops ["arrowstyle" ])
31
- data , col , _ = color .mpl_color2xcolor (
32
- data , obj .arrow_patch .get_ec ()
33
- )
34
- arrow_style .append (col )
35
-
36
- arrow_proto = "\\ draw[%s] (axis cs:%.15g,%.15g) " "-- (axis cs:%.15g,%.15g);\n "
37
- the_arrow = arrow_proto % (
38
- "," .join (arrow_style ),
39
- ann_xytext [0 ],
40
- ann_xytext [1 ],
41
- ann_xy [0 ],
42
- ann_xy [1 ],
43
- )
44
- content .append (the_arrow )
15
+ _annotation (obj , data , content )
45
16
46
17
# 1: coordinates
47
18
# 2: properties (shapes, rotation, etc)
@@ -57,60 +28,10 @@ def draw_text(data, obj):
57
28
# TODO fix this
58
29
scaling = 0.5 * size / data ["font size" ]
59
30
if scaling != 1.0 :
60
- properties .append ("scale=% .15g" % scaling )
31
+ properties .append ("scale={: .15g}" . format ( scaling ) )
61
32
62
33
if bbox is not None :
63
- bbox_style = bbox .get_boxstyle ()
64
- if bbox .get_fill ():
65
- data , fc , _ = color .mpl_color2xcolor (data , bbox .get_facecolor ())
66
- if fc :
67
- properties .append ("fill=%s" % fc )
68
- data , ec , _ = color .mpl_color2xcolor (data , bbox .get_edgecolor ())
69
- if ec :
70
- properties .append ("draw=%s" % ec )
71
- # XXX: This is ugly, too
72
- properties .append ("line width=%.15gpt" % (bbox .get_lw () * 0.4 ))
73
- properties .append ("inner sep=%.15gpt" % (bbox_style .pad * data ["font size" ]))
74
- # Rounded boxes
75
- if isinstance (bbox_style , mpl .patches .BoxStyle .Round ):
76
- properties .append ("rounded corners" )
77
- elif isinstance (bbox_style , mpl .patches .BoxStyle .RArrow ):
78
- data ["tikz libs" ].add ("shapes.arrows" )
79
- properties .append ("single arrow" )
80
- elif isinstance (bbox_style , mpl .patches .BoxStyle .LArrow ):
81
- data ["tikz libs" ].add ("shapes.arrows" )
82
- properties .append ("single arrow" )
83
- properties .append ("shape border rotate=180" )
84
- elif isinstance (bbox_style , mpl .patches .BoxStyle .DArrow ):
85
- data ["tikz libs" ].add ("shapes.arrows" )
86
- properties .append ("double arrow" )
87
- elif isinstance (bbox_style , mpl .patches .BoxStyle .Circle ):
88
- properties .append ("circle" )
89
- elif isinstance (bbox_style , mpl .patches .BoxStyle .Roundtooth ):
90
- properties .append ("decorate" )
91
- properties .append ("decoration={snake,amplitude=0.5,segment length=3}" )
92
- elif isinstance (bbox_style , mpl .patches .BoxStyle .Sawtooth ):
93
- properties .append ("decorate" )
94
- properties .append ("decoration={zigzag,amplitude=0.5,segment length=3}" )
95
- else :
96
- # TODO Round4
97
- assert isinstance (bbox_style , mpl .patches .BoxStyle .Square )
98
-
99
- # Line style
100
- if bbox .get_ls () == "dotted" :
101
- properties .append ("dotted" )
102
- elif bbox .get_ls () == "dashed" :
103
- properties .append ("dashed" )
104
- # TODO Check if there is there any way to extract the dashdot
105
- # pattern from matplotlib instead of hardcoding
106
- # an approximation?
107
- elif bbox .get_ls () == "dashdot" :
108
- properties .append (
109
- ("dash pattern=on %.3gpt off %.3gpt on " "%.3gpt off %.3gpt" )
110
- % (1.0 / scaling , 3.0 / scaling , 6.0 / scaling , 3.0 / scaling )
111
- )
112
- else :
113
- assert bbox .get_ls () == "solid"
34
+ _bbox (bbox , data , properties , scaling )
114
35
115
36
ha = obj .get_ha ()
116
37
va = obj .get_va ()
@@ -207,3 +128,93 @@ def _transform_positioning(ha, va):
207
128
"baseline" : "base" ,
208
129
}
209
130
return ("anchor=%s %s" % (va_mpl_to_tikz [va ], ha_mpl_to_tikz [ha ])).strip ()
131
+
132
+
133
+ def _annotation (obj , data , content ):
134
+ ann_xy = obj .xy
135
+ ann_xycoords = obj .xycoords
136
+ ann_xytext = obj .xyann
137
+ ann_textcoords = obj .anncoords
138
+ if ann_xycoords != "data" or ann_textcoords != "data" :
139
+ print (
140
+ "Warning: Anything else except for explicit positioning "
141
+ "is not supported for annotations yet :("
142
+ )
143
+ return data , content
144
+ else : # Create a basic tikz arrow
145
+ arrow_style = []
146
+ if obj .arrowprops is not None :
147
+ if obj .arrowprops ["arrowstyle" ] is not None :
148
+ if obj .arrowprops ["arrowstyle" ] in ["-" , "->" , "<-" , "<->" ]:
149
+ arrow_style .append (obj .arrowprops ["arrowstyle" ])
150
+ data , col , _ = color .mpl_color2xcolor (
151
+ data , obj .arrow_patch .get_ec ()
152
+ )
153
+ arrow_style .append (col )
154
+
155
+ arrow_proto = "\\ draw[%s] (axis cs:%.15g,%.15g) " "-- (axis cs:%.15g,%.15g);\n "
156
+ the_arrow = arrow_proto % (
157
+ "," .join (arrow_style ),
158
+ ann_xytext [0 ],
159
+ ann_xytext [1 ],
160
+ ann_xy [0 ],
161
+ ann_xy [1 ],
162
+ )
163
+ content .append (the_arrow )
164
+ return
165
+
166
+
167
+ def _bbox (bbox , data , properties , scaling ):
168
+ bbox_style = bbox .get_boxstyle ()
169
+ if bbox .get_fill ():
170
+ data , fc , _ = color .mpl_color2xcolor (data , bbox .get_facecolor ())
171
+ if fc :
172
+ properties .append ("fill=%s" % fc )
173
+ data , ec , _ = color .mpl_color2xcolor (data , bbox .get_edgecolor ())
174
+ if ec :
175
+ properties .append ("draw=%s" % ec )
176
+ # XXX: This is ugly, too
177
+ properties .append ("line width={:.15g}pt" .format (bbox .get_lw () * 0.4 ))
178
+ properties .append ("inner sep={:.15g}pt" .format (bbox_style .pad * data ["font size" ]))
179
+ # Rounded boxes
180
+ if isinstance (bbox_style , mpl .patches .BoxStyle .Round ):
181
+ properties .append ("rounded corners" )
182
+ elif isinstance (bbox_style , mpl .patches .BoxStyle .RArrow ):
183
+ data ["tikz libs" ].add ("shapes.arrows" )
184
+ properties .append ("single arrow" )
185
+ elif isinstance (bbox_style , mpl .patches .BoxStyle .LArrow ):
186
+ data ["tikz libs" ].add ("shapes.arrows" )
187
+ properties .append ("single arrow" )
188
+ properties .append ("shape border rotate=180" )
189
+ elif isinstance (bbox_style , mpl .patches .BoxStyle .DArrow ):
190
+ data ["tikz libs" ].add ("shapes.arrows" )
191
+ properties .append ("double arrow" )
192
+ elif isinstance (bbox_style , mpl .patches .BoxStyle .Circle ):
193
+ properties .append ("circle" )
194
+ elif isinstance (bbox_style , mpl .patches .BoxStyle .Roundtooth ):
195
+ properties .append ("decorate" )
196
+ properties .append ("decoration={snake,amplitude=0.5,segment length=3}" )
197
+ elif isinstance (bbox_style , mpl .patches .BoxStyle .Sawtooth ):
198
+ properties .append ("decorate" )
199
+ properties .append ("decoration={zigzag,amplitude=0.5,segment length=3}" )
200
+ else :
201
+ # TODO Round4
202
+ assert isinstance (bbox_style , mpl .patches .BoxStyle .Square )
203
+
204
+ # Line style
205
+ if bbox .get_ls () == "dotted" :
206
+ properties .append ("dotted" )
207
+ elif bbox .get_ls () == "dashed" :
208
+ properties .append ("dashed" )
209
+ # TODO Check if there is there any way to extract the dashdot
210
+ # pattern from matplotlib instead of hardcoding
211
+ # an approximation?
212
+ elif bbox .get_ls () == "dashdot" :
213
+ properties .append (
214
+ ("dash pattern=on %.3gpt off %.3gpt on " "%.3gpt off %.3gpt" )
215
+ % (1.0 / scaling , 3.0 / scaling , 6.0 / scaling , 3.0 / scaling )
216
+ )
217
+ else :
218
+ assert bbox .get_ls () == "solid"
219
+
220
+ return
0 commit comments