Skip to content

Commit 5498c98

Browse files
committed
better legend handling
1 parent 4e49c1b commit 5498c98

39 files changed

+302
-198
lines changed

matplotlib2tikz/axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def __init__(self, data, obj):
126126

127127
# actually print the thing
128128
if self.is_subplot:
129-
self.content.append("\\nextgroupplot")
129+
self.content.append("\n\\nextgroupplot")
130130
else:
131131
self.content.append("\\begin{axis}")
132132

@@ -405,7 +405,7 @@ def _subplot(self, obj, data):
405405
if "is_in_groupplot_env" not in data or not data["is_in_groupplot_env"]:
406406
self.content.append(
407407
"\\begin{{groupplot}}[group style="
408-
"{{group size={} by {}}}]\n".format(geom[1], geom[0])
408+
"{{group size={} by {}}}]".format(geom[1], geom[0])
409409
)
410410
data["is_in_groupplot_env"] = True
411411
data["pgfplots libs"].add("groupplots")

matplotlib2tikz/line2d.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,26 @@
99
from . import files
1010

1111

12-
def _is_in_legend(line):
12+
def _has_legend(axes):
13+
return axes.get_legend() is not None
14+
15+
16+
def _get_legend_text(line):
1317
"""Check if line is in legend.
1418
"""
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+
1526
label = line.get_label()
16-
try:
17-
leg = line.axes.get_legend()
18-
return label in [l.get_label() for l in leg.get_lines()]
19-
except AttributeError:
20-
return False
27+
d = dict(zip(keys, values))
28+
if label in d:
29+
return d[label]
30+
31+
return None
2132

2233

2334
def draw_line2d(data, obj):
@@ -73,21 +84,19 @@ def draw_line2d(data, obj):
7384

7485
# Check if a line is in a legend and forget it if not.
7586
# Fixes <https://github.com/nschloe/matplotlib2tikz/issues/167>.
76-
is_in_legend = _is_in_legend(obj)
77-
if not is_in_legend:
87+
legend_text = _get_legend_text(obj)
88+
if legend_text is None and _has_legend(obj.axes):
7889
addplot_options.append("forget plot")
7990

8091
# process options
8192
content.append("\\addplot ")
8293
if addplot_options:
83-
options = ", ".join(addplot_options)
84-
content.append("[" + options + "]\n")
94+
content.append("[{}]\n".format(", ".join(addplot_options)))
8595

8696
_table(obj, content, data)
8797

88-
if is_in_legend:
89-
label = obj.get_label()
90-
content.append("\\addlegendentry{{{}}}\n".format(label))
98+
if legend_text is not None:
99+
content.append("\\addlegendentry{{{}}}\n".format(legend_text))
91100

92101
return data, content
93102

@@ -153,7 +162,6 @@ def draw_linecollection(data, obj):
153162
data, cont, _, _ = mypath.draw_path(
154163
data, path, draw_options=options, simplify=False
155164
)
156-
157165
content.append(cont + "\n")
158166

159167
return data, content

matplotlib2tikz/patch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def draw_patchcollection(data, obj):
6868
def _is_in_legend(obj):
6969
label = obj.get_label()
7070
leg = obj.axes.get_legend()
71+
if leg is None:
72+
return False
7173
return label in [txt.get_text() for txt in leg.get_texts()]
7274

7375

matplotlib2tikz/path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def draw_path(data, path, draw_options=None, simplify=None):
110110
prev = vert[0:2]
111111

112112
do = "[{}]".format(", ".join(draw_options)) if draw_options else ""
113-
path_command = "\\path {} {};\n".format(do, "\n".join(nodes))
113+
path_command = "\\path {}\n{};\n".format(do, "\n".join(nodes))
114114

115115
return data, path_command, draw_options, is_area
116116

matplotlib2tikz/save.py

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

test/test_annotate_reference.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
y grid style={white!69.01960784313725!black},
99
ymin=-4, ymax=3
1010
]
11-
\addplot [semithick, blue, forget plot]
11+
\addplot [semithick, blue]
1212
table {%
1313
0 1
1414
0.2 0.309016994374947

test/test_barchart_errorbars_reference.tex

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,73 @@
1717
\draw[fill=red,draw opacity=0] (axis cs:0.125,0) rectangle (axis cs:0.375,5);
1818
\draw[fill=red,draw opacity=0] (axis cs:1.125,0) rectangle (axis cs:1.375,3);
1919
\draw[fill=red,draw opacity=0] (axis cs:2.125,0) rectangle (axis cs:2.375,1);
20-
\path [draw=black, line width=2.0pt] (axis cs:-0.25,0.9)
20+
\path [draw=black, line width=2.0pt]
21+
(axis cs:-0.25,0.9)
2122
--(axis cs:-0.25,1.1);
2223

23-
\path [draw=black, line width=2.0pt] (axis cs:0.75,1.8)
24+
\path [draw=black, line width=2.0pt]
25+
(axis cs:0.75,1.8)
2426
--(axis cs:0.75,2.2);
2527

26-
\path [draw=black, line width=2.0pt] (axis cs:1.75,2.5)
28+
\path [draw=black, line width=2.0pt]
29+
(axis cs:1.75,2.5)
2730
--(axis cs:1.75,3.5);
2831

29-
\path [draw=black, line width=2.0pt] (axis cs:0,2.6)
32+
\path [draw=black, line width=2.0pt]
33+
(axis cs:0,2.6)
3034
--(axis cs:0,3.4);
3135

32-
\path [draw=black, line width=2.0pt] (axis cs:1,1.8)
36+
\path [draw=black, line width=2.0pt]
37+
(axis cs:1,1.8)
3338
--(axis cs:1,2.2);
3439

35-
\path [draw=black, line width=2.0pt] (axis cs:2,3.5)
40+
\path [draw=black, line width=2.0pt]
41+
(axis cs:2,3.5)
3642
--(axis cs:2,4.5);
3743

38-
\path [draw=black, line width=2.0pt] (axis cs:0.25,4.9)
44+
\path [draw=black, line width=2.0pt]
45+
(axis cs:0.25,4.9)
3946
--(axis cs:0.25,5.1);
4047

41-
\path [draw=black, line width=2.0pt] (axis cs:1.25,2.8)
48+
\path [draw=black, line width=2.0pt]
49+
(axis cs:1.25,2.8)
4250
--(axis cs:1.25,3.2);
4351

44-
\path [draw=black, line width=2.0pt] (axis cs:2.25,0.9)
52+
\path [draw=black, line width=2.0pt]
53+
(axis cs:2.25,0.9)
4554
--(axis cs:2.25,1.1);
4655

47-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks, forget plot]
56+
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
4857
table {%
4958
-0.25 0.9
5059
0.75 1.8
5160
1.75 2.5
5261
};
53-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks, forget plot]
62+
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
5463
table {%
5564
-0.25 1.1
5665
0.75 2.2
5766
1.75 3.5
5867
};
59-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks, forget plot]
68+
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
6069
table {%
6170
0 2.6
6271
1 1.8
6372
2 3.5
6473
};
65-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks, forget plot]
74+
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
6675
table {%
6776
0 3.4
6877
1 2.2
6978
2 4.5
7079
};
71-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks, forget plot]
80+
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
7281
table {%
7382
0.25 4.9
7483
1.25 2.8
7584
2.25 0.9
7685
};
77-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks, forget plot]
86+
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
7887
table {%
7988
0.25 5.1
8089
1.25 3.2

test/test_basic_sin_reference.tex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
axis background/.style={fill=white!89.80392156862746!black},
88
axis line style={white},
99
legend cell align={left},
10-
legend entries={{sin},{cos}},
1110
legend style={at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black, fill=white!89.80392156862746!black},
1211
tick align=outside,
1312
tick pos=left,
@@ -44,6 +43,7 @@
4443
1.8 -0.951056516295154
4544
1.9 -0.587785252292473
4645
};
46+
\addlegendentry{sin}
4747
\addplot [very thick, color1, opacity=0.3, mark=*, mark size=3, mark options={solid}]
4848
table {%
4949
0 1
@@ -67,6 +67,7 @@
6767
1.8 0.309016994374947
6868
1.9 0.809016994374947
6969
};
70+
\addlegendentry{cos}
7071
\end{axis}
7172

7273
\end{tikzpicture}

test/test_boxplot_reference.tex

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,109 +10,109 @@
1010
y grid style={white!69.01960784313725!black},
1111
ymin=0.874561785006674, ymax=0.899947924923841
1212
]
13-
\addplot [black, forget plot]
13+
\addplot [black]
1414
table {%
1515
0.85 0.879496627112448
1616
1.15 0.879496627112448
1717
1.15 0.884126666796246
1818
0.85 0.884126666796246
1919
0.85 0.879496627112448
2020
};
21-
\addplot [black, forget plot]
21+
\addplot [black]
2222
table {%
2323
1 0.879496627112448
2424
1 0.875715700457454
2525
};
26-
\addplot [black, forget plot]
26+
\addplot [black]
2727
table {%
2828
1 0.884126666796246
2929
1 0.888867871101896
3030
};
31-
\addplot [black, forget plot]
31+
\addplot [black]
3232
table {%
3333
0.925 0.875715700457454
3434
1.075 0.875715700457454
3535
};
36-
\addplot [black, forget plot]
36+
\addplot [black]
3737
table {%
3838
0.925 0.888867871101896
3939
1.075 0.888867871101896
4040
};
41-
\addplot [black, forget plot]
41+
\addplot [black]
4242
table {%
4343
1.85 0.88877516922908
4444
2.15 0.88877516922908
4545
2.15 0.895051444894661
4646
1.85 0.895051444894661
4747
1.85 0.88877516922908
4848
};
49-
\addplot [black, forget plot]
49+
\addplot [black]
5050
table {%
5151
2 0.88877516922908
5252
2 0.884905350828982
5353
};
54-
\addplot [black, forget plot]
54+
\addplot [black]
5555
table {%
5656
2 0.895051444894661
5757
2 0.898794009473061
5858
};
59-
\addplot [black, forget plot]
59+
\addplot [black]
6060
table {%
6161
1.925 0.884905350828982
6262
2.075 0.884905350828982
6363
};
64-
\addplot [black, forget plot]
64+
\addplot [black]
6565
table {%
6666
1.925 0.898794009473061
6767
2.075 0.898794009473061
6868
};
69-
\addplot [black, mark=*, mark size=3, mark options={solid,fill opacity=0}, only marks, forget plot]
69+
\addplot [black, mark=*, mark size=3, mark options={solid,fill opacity=0}, only marks]
7070
table {%
7171
2 0.877726738991693
7272
};
73-
\addplot [black, forget plot]
73+
\addplot [black]
7474
table {%
7575
2.85 0.888622590566905
7676
3.15 0.888622590566905
7777
3.15 0.892127720467477
7878
2.85 0.892127720467477
7979
2.85 0.888622590566905
8080
};
81-
\addplot [black, forget plot]
81+
\addplot [black]
8282
table {%
8383
3 0.888622590566905
8484
3 0.884168322531585
8585
};
86-
\addplot [black, forget plot]
86+
\addplot [black]
8787
table {%
8888
3 0.892127720467477
8989
3 0.894515611281891
9090
};
91-
\addplot [black, forget plot]
91+
\addplot [black]
9292
table {%
9393
2.925 0.884168322531585
9494
3.075 0.884168322531585
9595
};
96-
\addplot [black, forget plot]
96+
\addplot [black]
9797
table {%
9898
2.925 0.894515611281891
9999
3.075 0.894515611281891
100100
};
101-
\addplot [black, mark=*, mark size=3, mark options={solid,fill opacity=0}, only marks, forget plot]
101+
\addplot [black, mark=*, mark size=3, mark options={solid,fill opacity=0}, only marks]
102102
table {%
103103
3 0.898104652708716
104104
};
105-
\addplot [color0, forget plot]
105+
\addplot [color0]
106106
table {%
107107
0.85 0.883074649623178
108108
1.15 0.883074649623178
109109
};
110-
\addplot [color0, forget plot]
110+
\addplot [color0]
111111
table {%
112112
1.85 0.892160329903995
113113
2.15 0.892160329903995
114114
};
115-
\addplot [color0, forget plot]
115+
\addplot [color0]
116116
table {%
117117
2.85 0.891012819071749
118118
3.15 0.891012819071749

test/test_colorbars.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,10 @@ def plot():
7979
def test():
8080
assert_equality(plot, "test_colorbars_reference.tex")
8181
return
82+
83+
84+
if __name__ == "__main__":
85+
import helpers
86+
87+
helpers.compare_mpl_latex(plot)
88+
# helpers.print_tree(plot())

0 commit comments

Comments
 (0)