Skip to content

Commit 248e6f1

Browse files
committed
properly add legend for scatter plots
1 parent 27a0e62 commit 248e6f1

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

matplotlib2tikz/path.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def draw_pathcollection(data, obj):
199199
content.append(fmt.format(*tuple(d)))
200200
content.append("};\n")
201201

202+
if legend_text is not None:
203+
content.append("\\addlegendentry{{{}}}\n".format(legend_text))
204+
202205
return data, content
203206

204207

test/test_legend_line_scatter.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
6+
from helpers import assert_equality
7+
8+
9+
def plot():
10+
fig = plt.figure()
11+
t = np.arange(5)
12+
np.random.seed(123)
13+
x = np.random.rand(5)
14+
plt.plot(t, x, label='line')
15+
plt.scatter(t, x, label='scatter')
16+
plt.legend()
17+
return fig
18+
19+
20+
def test():
21+
assert_equality(plot, __file__[:-3] + "_reference.tex")
22+
return
23+
24+
25+
if __name__ == "__main__":
26+
import helpers
27+
28+
helpers.compare_mpl_latex(plot)
29+
# helpers.print_tree(plot())
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
\begin{tikzpicture}
2+
3+
\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
4+
5+
\begin{axis}[
6+
legend cell align={left},
7+
legend style={at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black},
8+
tick align=outside,
9+
tick pos=left,
10+
x grid style={white!69.01960784313725!black},
11+
xmin=-0.229274193548387, xmax=4.22927419354839,
12+
y grid style={white!69.01960784313725!black},
13+
ymin=0.197382370004533, ymax=0.748938053345234
14+
]
15+
\addplot [only marks, draw=color0, fill=color0, colormap/viridis]
16+
table{%
17+
x y
18+
0 0.696469185597862
19+
1 0.286139334950379
20+
2 0.226851453564203
21+
3 0.551314769082891
22+
4 0.719468969785563
23+
};
24+
\addlegendentry{scatter}
25+
\addplot [semithick, color0]
26+
table {%
27+
0 0.696469185597862
28+
1 0.286139334950379
29+
2 0.226851453564203
30+
3 0.551314769082891
31+
4 0.719468969785563
32+
};
33+
\addlegendentry{line}
34+
\end{axis}
35+
36+
\end{tikzpicture}

0 commit comments

Comments
 (0)