Skip to content

Commit f4d1a96

Browse files
committed
Refactor arrowhead translation to be reuseable elsewhere
1 parent b4065a0 commit f4d1a96

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

tikzplotlib/_text.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import matplotlib as mpl
2+
from matplotlib.patches import ArrowStyle
23

34
from . import _color
45

@@ -164,6 +165,34 @@ def _parse_annotation_coords(ff, coords, xy):
164165
raise NotImplementedError
165166

166167

168+
def _get_arrow_style(obj, data):
169+
# get a style string from a FancyArrowPatch
170+
arrow_translate = {
171+
ArrowStyle._style_list["-"]: ["-"],
172+
ArrowStyle._style_list["->"]: ["->"],
173+
ArrowStyle._style_list["<-"]: ["<-"],
174+
ArrowStyle._style_list["<->"]: ["<->"],
175+
ArrowStyle._style_list["|-|"]: ["|-|"],
176+
ArrowStyle._style_list["-|>"]: ["-latex"],
177+
ArrowStyle._style_list["<|-"]: ["latex-"],
178+
ArrowStyle._style_list["<|-|>"]: ["latex-latex"],
179+
ArrowStyle._style_list["]-["]: ["|-|"],
180+
ArrowStyle._style_list["-["]: ["-|"],
181+
ArrowStyle._style_list["]-"]: ["|-"],
182+
ArrowStyle._style_list["fancy"]: ["-latex", "very thick"],
183+
ArrowStyle._style_list["simple"]: ["-latex", "very thick"],
184+
ArrowStyle._style_list["wedge"]: ["-latex", "very thick"],
185+
}
186+
style_cls = type(obj.get_arrowstyle())
187+
try:
188+
style = arrow_translate[style_cls]
189+
except KeyError:
190+
raise NotImplementedError("Unknown arrow style {}".format(style_cls))
191+
else:
192+
data, col, _ = _color.mpl_color2xcolor(data, obj.get_ec())
193+
return style + [col]
194+
195+
167196
def _annotation(obj, data, content):
168197
ann_xy = obj.xy
169198
ann_xycoords = obj.xycoords
@@ -190,35 +219,9 @@ def _annotation(obj, data, content):
190219
# Anything else except for explicit positioning is not supported yet
191220
return obj.get_position()
192221

193-
# Create a basic tikz arrow
194-
arrow_translate = {
195-
"-": ["-"],
196-
"->": ["->"],
197-
"<-": ["<-"],
198-
"<->": ["<->"],
199-
"|-|": ["|-|"],
200-
"-|>": ["-latex"],
201-
"<|-": ["latex-"],
202-
"<|-|>": ["latex-latex"],
203-
"]-[": ["|-|"],
204-
"-[": ["-|"],
205-
"]-": ["|-"],
206-
"fancy": ["-latex", "very thick"],
207-
"simple": ["-latex", "very thick"],
208-
"wedge": ["-latex", "very thick"],
209-
}
210-
arrow_style = []
211-
if obj.arrowprops is not None:
212-
if obj.arrowprops["arrowstyle"] is not None:
213-
if obj.arrowprops["arrowstyle"] in arrow_translate:
214-
arrow_style += arrow_translate[obj.arrowprops["arrowstyle"]]
215-
data, col, _ = _color.mpl_color2xcolor(data, obj.arrow_patch.get_ec())
216-
arrow_style.append(col)
217-
218-
if arrow_style:
219-
the_arrow = ("\\draw[{}] {} -- {};\n").format(
220-
",".join(arrow_style), text_pos, xy_pos
221-
)
222+
if obj.arrow_patch:
223+
style = ",".join(_get_arrow_style(obj.arrow_patch, data))
224+
the_arrow = ("\\draw[{}] {} -- {};\n").format(style, text_pos, xy_pos)
222225
content.append(the_arrow)
223226
return text_pos
224227

0 commit comments

Comments
 (0)