Skip to content

Commit 86991b3

Browse files
authored
Merge pull request #183 from danielhkl/master
Legend colors and position
2 parents 09a8f58 + 8a69c67 commit 86991b3

File tree

7 files changed

+24
-5
lines changed

7 files changed

+24
-5
lines changed

matplotlib2tikz/legend.py

Lines changed: 16 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,16 @@ def draw_legend(data, obj):
193199
'legend columns={}'.format(obj._ncol)
194200
)
195201

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

matplotlib2tikz/save.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def get_tikz_code(
124124
data['pgfplots libs'] = set()
125125
data['font size'] = textsize
126126
data['custom colors'] = {}
127+
data['legend colors'] = []
127128
data['extra tikzpicture parameters'] = extra_tikzpicture_parameters
128129
# rectangle_legends is used to keep track of which rectangles have already
129130
# had \addlegendimage added. There should be only one \addlegenimage per
@@ -311,6 +312,8 @@ def _recurse(data, obj):
311312
content.extend(cont, child.get_zorder())
312313
elif isinstance(child, mpl.legend.Legend):
313314
data = legend.draw_legend(data, child)
315+
if data['legend colors']:
316+
content.extend(data['legend colors'], 0)
314317
elif isinstance(
315318
child,
316319
(

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

test/test_legends2.py

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

3333
def test():
3434
phash = helpers.Phash(plot())
35-
assert phash.phash == '7b447a5a62d4852f', phash.get_details()
35+
assert phash.phash == '7b447a5262d4952f', phash.get_details()
3636
return

0 commit comments

Comments
 (0)