Skip to content

Commit f8d4405

Browse files
committed
tidied up pull request for legend colors and legend position
1 parent 30e48fc commit f8d4405

File tree

6 files changed

+23
-4
lines changed

6 files changed

+23
-4
lines changed

matplotlib2tikz/legend.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ def draw_legend(data, obj):
148148
position = [0.5, 0.5]
149149
anchor = 'center'
150150

151+
# In case of given position via bbox_to_anchor parameter the center
152+
# of legend is changed as follows:
153+
if obj._bbox_to_anchor:
154+
bbox_center = obj.get_bbox_to_anchor()._bbox._points[1]
155+
position = [bbox_center[0], bbox_center[1]]
156+
151157
legend_style = []
152158
if position:
153159
legend_style.append('at={(%.15g,%.15g)}' % (position[0], position[1]))
@@ -193,6 +199,17 @@ def draw_legend(data, obj):
193199
'legend columns={}'.format(obj._ncol)
194200
)
195201

202+
# Set color of lines in legend
203+
data['legend colors'] = []
204+
for handle in obj.legendHandles:
205+
try:
206+
data, legend_color, _ = mycol.mpl_color2xcolor(data,
207+
handle.get_color())
208+
data['legend colors'].append('\\addlegendimage{no markers, %s}\n'
209+
% legend_color)
210+
except(AttributeError):
211+
data['legend colors'] = []
212+
196213
# Write styles to data
197214
if legend_style:
198215
style = 'legend style={%s}' % ', '.join(legend_style)

matplotlib2tikz/save.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ def _recurse(data, obj):
311311
content.extend(cont, child.get_zorder())
312312
elif isinstance(child, mpl.legend.Legend):
313313
data = legend.draw_legend(data, child)
314+
if data['legend colors']:
315+
content.extend(data['legend colors'], 0)
314316
elif isinstance(
315317
child,
316318
(

test/test_legend_best_location.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ def plot():
6262

6363
def test():
6464
phash = helpers.Phash(plot())
65-
assert phash.phash == '839f990d0e877c1d', phash.get_details()
65+
assert phash.phash == '879d990d0e8f7c1c', phash.get_details()
6666
return

test/test_legend_columns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def plot():
1515

1616
def test():
1717
phash = helpers.Phash(plot())
18-
assert phash.phash == '8386de99666939a9', phash.get_details()
18+
assert phash.phash == '8387de99666938a9', phash.get_details()
1919
return
2020

2121

test/test_legend_labels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def plot():
2424

2525
def test():
2626
phash = helpers.Phash(plot())
27-
assert phash.phash == 'eb785e0aaed68194', phash.get_details()
27+
assert phash.phash == 'eb78de0aaed6c110', phash.get_details()
2828
return
2929

3030

test/test_legends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def plot():
3131

3232
def test():
3333
phash = helpers.Phash(plot())
34-
assert phash.phash == '7f7ca18386d10eaa', phash.get_details()
34+
assert phash.phash == '7f7ca18386d10e8b', phash.get_details()
3535
return
3636

3737

0 commit comments

Comments
 (0)