@@ -37,30 +37,17 @@ def _is_in_legend(obj):
37
37
return label in [txt .get_text () for txt in leg .get_texts ()]
38
38
39
39
40
- def legendimage ( func ):
40
+ def _patch_legend ( obj , draw_options , legend_type ):
41
41
""" 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
+ )
42
49
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
64
51
65
52
66
53
def draw_patchcollection (data , obj ):
@@ -97,27 +84,22 @@ def draw_patchcollection(data, obj):
97
84
)
98
85
content .append (cont )
99
86
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 )
110
90
111
91
return data , content
112
92
113
- @ legendimage
93
+
114
94
def _draw_polygon (data , obj , draw_options ):
115
95
data , content , _ , is_area = mypath .draw_path (
116
96
data , obj .get_path (), draw_options = draw_options
117
97
)
118
98
legend_type = "area legend" if is_area else "line legend"
99
+ legend = _patch_legend (obj , draw_options , legend_type )
100
+ content += legend
119
101
120
- return data , content , legend_type
102
+ return data , content
121
103
122
104
123
105
def _draw_rectangle (data , obj , draw_options ):
@@ -163,7 +145,6 @@ def _draw_rectangle(data, obj, draw_options):
163
145
return data , cont
164
146
165
147
166
- @legendimage
167
148
def _draw_ellipse (data , obj , draw_options ):
168
149
"""Return the PGFPlots code for ellipses.
169
150
"""
@@ -177,7 +158,7 @@ def _draw_ellipse(data, obj, draw_options):
177
158
fmt = "rotate around={{" + ff + ":(axis cs:" + ff + "," + ff + ")}}"
178
159
draw_options .append (fmt .format (obj .angle , x , y ))
179
160
180
- cont = (
161
+ content = (
181
162
"\\ draw[{}] (axis cs:"
182
163
+ ff
183
164
+ ","
@@ -186,19 +167,23 @@ def _draw_ellipse(data, obj, draw_options):
186
167
+ ff
187
168
+ " and "
188
169
+ ff
189
- + ");"
170
+ + ");\n "
190
171
).format ("," .join (draw_options ), x , y , 0.5 * obj .width , 0.5 * obj .height )
191
172
192
- return data , cont , "area legend"
173
+ legend = _patch_legend (obj , draw_options , "area legend" )
174
+ content += legend
175
+
176
+ return data , content
193
177
194
178
195
- @legendimage
196
179
def _draw_circle (data , obj , draw_options ):
197
180
"""Return the PGFPlots code for circles.
198
181
"""
199
182
x , y = obj .center
200
183
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