Skip to content

Commit 7763c2b

Browse files
authored
Merge pull request #366 from eric-wieser/FancyArrowPatch
Add support for FancyArrowPatch objects
2 parents b4065a0 + 9d86cfc commit 7763c2b

File tree

6 files changed

+103
-46
lines changed

6 files changed

+103
-46
lines changed

test/test_annotate_reference.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
4.6 -0.809016994374946
3939
4.8 0.30901699437495
4040
};
41-
\draw[->,red] (axis cs:4.5,1.5) -- (axis cs:4,1);
41+
\draw[->,draw=red] (axis cs:4.5,1.5) -- (axis cs:4,1);
4242
\draw (axis cs:4.5,1.5) node[
4343
scale=0.5,
4444
anchor=base west,
4545
text=black,
4646
rotate=0.0
4747
]{text};
48-
\draw[->,black] (axis cs:0,1) ++(-50pt,30pt) -- (axis cs:0,1);
48+
\draw[->,draw=black] (axis cs:0,1) ++(-50pt,30pt) -- (axis cs:0,1);
4949
\draw (axis cs:0,1) ++(-50pt,30pt) node[
5050
scale=0.5,
5151
anchor=base west,

test/test_arrows_reference.tex

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
\draw[draw=none,fill=color0] (axis cs:7.2,2.8) circle (0.2);
2727
\draw[draw=none,fill=color0] (axis cs:7.2,1.8) circle (0.2);
2828
\draw[draw=none,fill=color0] (axis cs:7.2,0.8) circle (0.2);
29-
\draw[-,black] (axis cs:2,6.8) -- (axis cs:3.2,6.8);
29+
\draw[-,draw=black] (axis cs:2,6.8) -- (axis cs:3.2,6.8);
3030
\draw (axis cs:2,6.8) node[
3131
scale=0.7,
3232
fill=white,
@@ -37,7 +37,7 @@
3737
text=black,
3838
rotate=0.0
3939
]{-};
40-
\draw[->,black] (axis cs:2,5.8) -- (axis cs:3.2,5.8);
40+
\draw[->,draw=black] (axis cs:2,5.8) -- (axis cs:3.2,5.8);
4141
\draw (axis cs:2,5.8) node[
4242
scale=0.7,
4343
fill=white,
@@ -48,7 +48,7 @@
4848
text=black,
4949
rotate=0.0
5050
]{-$>$};
51-
\draw[-|,black] (axis cs:2,4.8) -- (axis cs:3.2,4.8);
51+
\draw[-|,draw=black] (axis cs:2,4.8) -- (axis cs:3.2,4.8);
5252
\draw (axis cs:2,4.8) node[
5353
scale=0.7,
5454
fill=white,
@@ -59,7 +59,7 @@
5959
text=black,
6060
rotate=0.0
6161
]{-[};
62-
\draw[-latex,black] (axis cs:2,3.8) -- (axis cs:3.2,3.8);
62+
\draw[-latex,draw=black] (axis cs:2,3.8) -- (axis cs:3.2,3.8);
6363
\draw (axis cs:2,3.8) node[
6464
scale=0.7,
6565
fill=white,
@@ -70,7 +70,7 @@
7070
text=black,
7171
rotate=0.0
7272
]{-$|$$>$};
73-
\draw[<-,black] (axis cs:2,2.8) -- (axis cs:3.2,2.8);
73+
\draw[<-,draw=black] (axis cs:2,2.8) -- (axis cs:3.2,2.8);
7474
\draw (axis cs:2,2.8) node[
7575
scale=0.7,
7676
fill=white,
@@ -81,7 +81,7 @@
8181
text=black,
8282
rotate=0.0
8383
]{$<$-};
84-
\draw[<->,black] (axis cs:2,1.8) -- (axis cs:3.2,1.8);
84+
\draw[<->,draw=black] (axis cs:2,1.8) -- (axis cs:3.2,1.8);
8585
\draw (axis cs:2,1.8) node[
8686
scale=0.7,
8787
fill=white,
@@ -92,7 +92,7 @@
9292
text=black,
9393
rotate=0.0
9494
]{$<$-$>$};
95-
\draw[latex-,black] (axis cs:2,0.8) -- (axis cs:3.2,0.8);
95+
\draw[latex-,draw=black] (axis cs:2,0.8) -- (axis cs:3.2,0.8);
9696
\draw (axis cs:2,0.8) node[
9797
scale=0.7,
9898
fill=white,
@@ -103,7 +103,7 @@
103103
text=black,
104104
rotate=0.0
105105
]{$<$$|$-};
106-
\draw[latex-latex,black] (axis cs:6,6.8) -- (axis cs:7.2,6.8);
106+
\draw[latex-latex,draw=black] (axis cs:6,6.8) -- (axis cs:7.2,6.8);
107107
\draw (axis cs:6,6.8) node[
108108
scale=0.7,
109109
fill=white,
@@ -114,7 +114,7 @@
114114
text=black,
115115
rotate=0.0
116116
]{$<$$|$-$|$$>$};
117-
\draw[|-,black] (axis cs:6,5.8) -- (axis cs:7.2,5.8);
117+
\draw[|-,draw=black] (axis cs:6,5.8) -- (axis cs:7.2,5.8);
118118
\draw (axis cs:6,5.8) node[
119119
scale=0.7,
120120
fill=white,
@@ -125,7 +125,7 @@
125125
text=black,
126126
rotate=0.0
127127
]{]-};
128-
\draw[|-|,black] (axis cs:6,4.8) -- (axis cs:7.2,4.8);
128+
\draw[|-|,draw=black] (axis cs:6,4.8) -- (axis cs:7.2,4.8);
129129
\draw (axis cs:6,4.8) node[
130130
scale=0.7,
131131
fill=white,
@@ -136,7 +136,7 @@
136136
text=black,
137137
rotate=0.0
138138
]{]-[};
139-
\draw[-latex,very thick,black] (axis cs:6,3.8) -- (axis cs:7.2,3.8);
139+
\draw[-latex,very thick,draw=black] (axis cs:6,3.8) -- (axis cs:7.2,3.8);
140140
\draw (axis cs:6,3.8) node[
141141
scale=0.7,
142142
fill=white,
@@ -147,7 +147,7 @@
147147
text=black,
148148
rotate=0.0
149149
]{fancy};
150-
\draw[-latex,very thick,black] (axis cs:6,2.8) -- (axis cs:7.2,2.8);
150+
\draw[-latex,very thick,draw=black] (axis cs:6,2.8) -- (axis cs:7.2,2.8);
151151
\draw (axis cs:6,2.8) node[
152152
scale=0.7,
153153
fill=white,
@@ -158,7 +158,7 @@
158158
text=black,
159159
rotate=0.0
160160
]{simple};
161-
\draw[-latex,very thick,black] (axis cs:6,1.8) -- (axis cs:7.2,1.8);
161+
\draw[-latex,very thick,draw=black] (axis cs:6,1.8) -- (axis cs:7.2,1.8);
162162
\draw (axis cs:6,1.8) node[
163163
scale=0.7,
164164
fill=white,
@@ -169,7 +169,7 @@
169169
text=black,
170170
rotate=0.0
171171
]{wedge};
172-
\draw[|-|,black] (axis cs:6,0.8) -- (axis cs:7.2,0.8);
172+
\draw[|-|,draw=black] (axis cs:6,0.8) -- (axis cs:7.2,0.8);
173173
\draw (axis cs:6,0.8) node[
174174
scale=0.7,
175175
fill=white,

test/test_patches.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33

44
def plot():
5-
from matplotlib.patches import Circle, Ellipse, Polygon, Rectangle, Wedge
5+
from matplotlib.patches import (
6+
Circle,
7+
Ellipse,
8+
Polygon,
9+
Rectangle,
10+
Wedge,
11+
FancyArrowPatch,
12+
)
613
from matplotlib.collections import PatchCollection
14+
from matplotlib.path import Path
715
from matplotlib import pyplot as plt
816
import numpy as np
917
import matplotlib as mpl
@@ -57,6 +65,18 @@ def plot():
5765
circle = Circle(xy=[0.0, 1.0], radius=0.5, color="r", alpha=0.4)
5866
ax.add_patch(circle)
5967

68+
arrow = FancyArrowPatch(posA=[0.25, 0.25], posB=[0.5, 0.25], arrowstyle="->")
69+
ax.add_patch(arrow)
70+
71+
curved_arrow = FancyArrowPatch(
72+
path=Path(
73+
[(0.3, 0.3), (0.5, 1.0), (1.0, 0.8), (0.8, 0.3)],
74+
[Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4],
75+
),
76+
arrowstyle="-|>",
77+
)
78+
ax.add_patch(curved_arrow)
79+
6080
plt.colorbar(p)
6181

6282
return fig

test/test_patches_reference.tex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@
198198

199199
\draw[draw=none,fill=color0,fill opacity=0.4,rotate around={45:(axis cs:1,0.5)}] (axis cs:1,0.5) ellipse (0.5 and 0.25);
200200
\draw[draw=red,fill=red,opacity=0.4] (axis cs:0,1) circle (0.5);
201+
\draw[->,draw=black] (axis cs:0.25,0.25) -- (axis cs:0.5,0.25);
202+
\path [draw=black, -latex, draw=black]
203+
(axis cs:0.3,0.3)
204+
.. controls (axis cs:0.5,1) and (axis cs:1,0.8) .. (axis cs:0.8,0.3);
201205
\end{axis}
202206

203207
\end{tikzpicture}

tikzplotlib/_patch.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
import matplotlib as mpl
22

33
from . import _path as mypath
4+
from ._text import _get_arrow_style
45

56

67
def draw_patch(data, obj):
78
"""Return the PGFPlots code for patches.
89
"""
10+
if isinstance(obj, mpl.patches.FancyArrowPatch):
11+
data, draw_options = mypath.get_draw_options(
12+
data,
13+
obj,
14+
obj.get_edgecolor(),
15+
# get_fillcolor for the arrow refers to the head, not the path
16+
None,
17+
obj.get_linestyle(),
18+
obj.get_linewidth(),
19+
obj.get_hatch(),
20+
)
21+
return _draw_fancy_arrow(data, obj, draw_options)
22+
923
# Gather the draw options.
1024
data, draw_options = mypath.get_draw_options(
1125
data,
@@ -184,3 +198,19 @@ def _draw_circle(data, obj, draw_options):
184198
content += _patch_legend(obj, draw_options, "area legend")
185199

186200
return data, content
201+
202+
203+
def _draw_fancy_arrow(data, obj, draw_options):
204+
style = _get_arrow_style(obj, data)
205+
ff = data["float format"]
206+
if obj._posA_posB is not None:
207+
posA, posB = obj._posA_posB
208+
content = "\\draw[{{}}] (axis cs:{ff},{ff}) -- (axis cs:{ff},{ff});\n".format(
209+
ff=ff
210+
).format(",".join(style), *posA, *posB)
211+
else:
212+
data, content, _, _ = mypath.draw_path(
213+
data, obj._path_original, draw_options=draw_options + style
214+
)
215+
content += _patch_legend(obj, draw_options, "line legend")
216+
return data, content

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 + ["draw=" + 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)