Skip to content

Commit de539b2

Browse files
committed
make text() less complex
1 parent 77c9d88 commit de539b2

File tree

1 file changed

+93
-82
lines changed

1 file changed

+93
-82
lines changed

matplotlib2tikz/text.py

+93-82
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,7 @@ def draw_text(data, obj):
1212
properties = []
1313
style = []
1414
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)
4516

4617
# 1: coordinates
4718
# 2: properties (shapes, rotation, etc)
@@ -57,60 +28,10 @@ def draw_text(data, obj):
5728
# TODO fix this
5829
scaling = 0.5 * size / data["font size"]
5930
if scaling != 1.0:
60-
properties.append("scale=%.15g" % scaling)
31+
properties.append("scale={:.15g}".format(scaling))
6132

6233
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)
11435

11536
ha = obj.get_ha()
11637
va = obj.get_va()
@@ -207,3 +128,93 @@ def _transform_positioning(ha, va):
207128
"baseline": "base",
208129
}
209130
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

Comments
 (0)