Skip to content

Commit 92eab8c

Browse files
committed
Removed legend decorator. Bad idea..
1 parent 83afcb9 commit 92eab8c

File tree

1 file changed

+28
-43
lines changed

1 file changed

+28
-43
lines changed

tikzplotlib/_patch.py

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,17 @@ def _is_in_legend(obj):
3737
return label in [txt.get_text() for txt in leg.get_texts()]
3838

3939

40-
def legendimage(func):
40+
def _patch_legend(obj, draw_options, legend_type):
4141
""" Decorator for handling legend of mpl.Patch """
42+
legend = "\n"
43+
if _is_in_legend(obj):
44+
# Unfortunately, patch legend entries need \addlegendimage in Pgfplots.
45+
do = ", ".join([legend_type] + draw_options) if draw_options else ""
46+
legend = "\\addlegendimage{{{}}}\n\\addlegendentry{{{}}}\n\n".format(
47+
do, obj.get_label()
48+
)
4249

43-
@wraps(func)
44-
def wrapper(data, obj, draw_options):
45-
data, content, legend_type = func(data, obj, draw_options)
46-
47-
legend = "\n"
48-
if _is_in_legend(obj):
49-
# Unfortunately, patch legend entries need \addlegendimage in Pgfplots.
50-
do = ", ".join([legend_type] + draw_options) if draw_options else ""
51-
legend = "\\addlegendimage{{{}}}\n\\addlegendentry{{{}}}\n\n".format(
52-
do, obj.get_label()
53-
)
54-
55-
# content might be str or list
56-
try:
57-
content += legend
58-
except TypeError:
59-
content += [legend]
60-
61-
return data, content
62-
63-
return wrapper
50+
return legend
6451

6552

6653
def draw_patchcollection(data, obj):
@@ -97,27 +84,22 @@ def draw_patchcollection(data, obj):
9784
)
9885
content.append(cont)
9986

100-
if _is_in_legend(obj):
101-
# Unfortunately, patch legend entries need \addlegendimage in Pgfplots.
102-
tpe = "area legend" if is_area else "line legend"
103-
do = ", ".join([tpe] + draw_options) if draw_options else ""
104-
content += [
105-
"\\addlegendimage{{{}}}\n".format(do),
106-
"\\addlegendentry{{{}}}\n\n".format(obj.get_label()),
107-
]
108-
else:
109-
content.append("\n")
87+
legend_type = "area legend" if is_area else "line legend"
88+
legend = _patch_legend(obj, draw_options, legend_type)
89+
content.append(legend)
11090

11191
return data, content
11292

113-
@legendimage
93+
11494
def _draw_polygon(data, obj, draw_options):
11595
data, content, _, is_area = mypath.draw_path(
11696
data, obj.get_path(), draw_options=draw_options
11797
)
11898
legend_type = "area legend" if is_area else "line legend"
99+
legend = _patch_legend(obj, draw_options, legend_type)
100+
content += legend
119101

120-
return data, content, legend_type
102+
return data, content
121103

122104

123105
def _draw_rectangle(data, obj, draw_options):
@@ -163,7 +145,6 @@ def _draw_rectangle(data, obj, draw_options):
163145
return data, cont
164146

165147

166-
@legendimage
167148
def _draw_ellipse(data, obj, draw_options):
168149
"""Return the PGFPlots code for ellipses.
169150
"""
@@ -177,7 +158,7 @@ def _draw_ellipse(data, obj, draw_options):
177158
fmt = "rotate around={{" + ff + ":(axis cs:" + ff + "," + ff + ")}}"
178159
draw_options.append(fmt.format(obj.angle, x, y))
179160

180-
cont = (
161+
content = (
181162
"\\draw[{}] (axis cs:"
182163
+ ff
183164
+ ","
@@ -186,19 +167,23 @@ def _draw_ellipse(data, obj, draw_options):
186167
+ ff
187168
+ " and "
188169
+ ff
189-
+ ");"
170+
+ ");\n"
190171
).format(",".join(draw_options), x, y, 0.5 * obj.width, 0.5 * obj.height)
191172

192-
return data, cont, "area legend"
173+
legend = _patch_legend(obj, draw_options, "area legend")
174+
content += legend
175+
176+
return data, content
193177

194178

195-
@legendimage
196179
def _draw_circle(data, obj, draw_options):
197180
"""Return the PGFPlots code for circles.
198181
"""
199182
x, y = obj.center
200183
ff = data["float format"]
201-
cont = ("\\draw[{}] (axis cs:" + ff + "," + ff + ") circle (" + ff + ");").format(
202-
",".join(draw_options), x, y, obj.get_radius()
203-
)
204-
return data, cont, "area legend"
184+
content = (
185+
"\\draw[{}] (axis cs:" + ff + "," + ff + ") circle (" + ff + ");\n"
186+
).format(",".join(draw_options), x, y, obj.get_radius())
187+
legend = _patch_legend(obj, draw_options, "area legend")
188+
content += legend
189+
return data, content

0 commit comments

Comments
 (0)