Skip to content

Commit 793f27d

Browse files
committed
properly forget paths from legends
1 parent c209462 commit 793f27d

File tree

4 files changed

+35
-28
lines changed

4 files changed

+35
-28
lines changed

matplotlib2tikz/line2d.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,7 @@
88
from . import path as mypath
99
from . import files
1010

11-
12-
def _has_legend(axes):
13-
return axes.get_legend() is not None
14-
15-
16-
def _get_legend_text(line):
17-
"""Check if line is in legend.
18-
"""
19-
leg = line.axes.get_legend()
20-
if leg is None:
21-
return None
22-
23-
keys = [l.get_label() for l in leg.get_lines()]
24-
values = [l.get_text() for l in leg.texts]
25-
26-
label = line.get_label()
27-
d = dict(zip(keys, values))
28-
if label in d:
29-
return d[label]
30-
31-
return None
11+
from .util import get_legend_text, has_legend
3212

3313

3414
def draw_line2d(data, obj):
@@ -84,8 +64,8 @@ def draw_line2d(data, obj):
8464

8565
# Check if a line is in a legend and forget it if not.
8666
# Fixes <https://github.com/nschloe/matplotlib2tikz/issues/167>.
87-
legend_text = _get_legend_text(obj)
88-
if legend_text is None and _has_legend(obj.axes):
67+
legend_text = get_legend_text(obj)
68+
if legend_text is None and has_legend(obj.axes):
8969
addplot_options.append("forget plot")
9070

9171
# process options

matplotlib2tikz/path.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from . import color
77
from .axes import _mpl_cmap2pgf_cmap
88

9+
from .util import has_legend, get_legend_text
10+
911

1012
def draw_path(data, path, draw_options=None, simplify=None):
1113
"""Adds code for drawing an ordinary path in PGFPlots (TikZ).
@@ -159,10 +161,11 @@ def draw_pathcollection(data, obj):
159161

160162
if obj.get_cmap():
161163
mycolormap, is_custom_cmap = _mpl_cmap2pgf_cmap(obj.get_cmap(), data)
162-
if is_custom_cmap:
163-
draw_options.append("colormap=" + mycolormap)
164-
else:
165-
draw_options.append("colormap/" + mycolormap)
164+
draw_options.append("colormap" + ("=" if is_custom_cmap else "/") + mycolormap)
165+
166+
legend_text = get_legend_text(obj)
167+
if legend_text is None and has_legend(obj.axes):
168+
draw_options.append("forget plot")
166169

167170
for path in obj.get_paths():
168171
if is_contour:

matplotlib2tikz/save.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def get_tikz_code(
224224
\\usepackage{{pgfplots}}
225225
\\usepgfplotslibrary{{groupplots}}
226226
\\pgfplotsset{{compat=newest}}
227-
\\DeclareUnicodeCharacter{{2212}}{{-}}
227+
\\DeclareUnicodeCharacter{{2212}}{{}}
228228
\\begin{{document}}
229229
{}
230230
\\end{{document}}""".format(

matplotlib2tikz/util.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
4+
5+
def has_legend(axes):
6+
return axes.get_legend() is not None
7+
8+
9+
def get_legend_text(obj):
10+
"""Check if line is in legend.
11+
"""
12+
leg = obj.axes.get_legend()
13+
if leg is None:
14+
return None
15+
16+
keys = [l.get_label() for l in leg.legendHandles if l is not None]
17+
values = [l.get_text() for l in leg.texts]
18+
19+
label = obj.get_label()
20+
d = dict(zip(keys, values))
21+
if label in d:
22+
return d[label]
23+
24+
return None

0 commit comments

Comments
 (0)