Skip to content

Commit 850907b

Browse files
authored
Merge pull request #283 from nschloe/legend-line-scatter
properly add legend for scatter plots
2 parents 27a0e62 + f907431 commit 850907b

13 files changed

+83
-135
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_image_plot_lower.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def plot():
2323
ax = plt.axes([0, 0, 1, 1], frameon=False)
2424
ax.set_axis_off()
2525
plt.imshow(img, cmap="viridis", origin="lower")
26-
# Set the current color map to HSV.
27-
plt.hsv()
26+
# Setting the current color map to HSV messes up other plots
27+
# plt.hsv()
2828
plt.colorbar()
2929
return fig
3030

test/test_image_plot_lower_reference.tex

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
\begin{axis}[
44
colorbar,
55
colorbar style={ylabel={}},
6-
colormap={mymap}{[1pt]
7-
rgb(0pt)=(1,0,0);
8-
rgb(10pt)=(1,0.9375,0);
9-
rgb(11pt)=(0.96875,1,0);
10-
rgb(21pt)=(0.03125,1,0);
11-
rgb(22pt)=(0,1,0.0625);
12-
rgb(32pt)=(0,1,1);
13-
rgb(42pt)=(0,0.0625,1);
14-
rgb(43pt)=(0.03125,0,1);
15-
rgb(53pt)=(0.96875,0,1);
16-
rgb(54pt)=(1,0,0.9375);
17-
rgb(63pt)=(1,0,0.09375)
18-
},
6+
colormap/viridis,
197
hide x axis,
208
hide y axis,
219
point meta max=1,

test/test_image_plot_upper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ def plot():
2222

2323
plt.imshow(img, cmap="viridis", origin="upper")
2424

25-
# Set the current color map to HSV.
26-
plt.hsv()
25+
# Setting the current color map to HSV messes up other plots
26+
# plt.hsv()
2727
plt.colorbar()
2828
return fig
2929

3030

3131
# TODO reintroduce
3232
# from helpers import assert_equality
3333
# def test():
34-
# assert_equality(plot, "test_image_plot_lower_reference.tex")
34+
# assert_equality(plot, __file__[:-3] + "_reference.tex")
3535
# return

test/test_image_plot_upper_reference.tex

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
\begin{axis}[
44
colorbar,
55
colorbar style={ylabel={}},
6-
colormap={mymap}{[1pt]
7-
rgb(0pt)=(1,0,0);
8-
rgb(10pt)=(1,0.9375,0);
9-
rgb(11pt)=(0.96875,1,0);
10-
rgb(21pt)=(0.03125,1,0);
11-
rgb(22pt)=(0,1,0.0625);
12-
rgb(32pt)=(0,1,1);
13-
rgb(42pt)=(0,0.0625,1);
14-
rgb(43pt)=(0.03125,0,1);
15-
rgb(53pt)=(0.96875,0,1);
16-
rgb(54pt)=(1,0,0.9375);
17-
rgb(63pt)=(1,0,0.09375)
18-
},
6+
colormap/viridis,
197
hide x axis,
208
hide y axis,
219
point meta max=1,

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 = t
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.97)}, anchor=north 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.239285714285714, ymax=4.23928571428571
14+
]
15+
\addplot [only marks, draw=color0, fill=color0, colormap/viridis]
16+
table{%
17+
x y
18+
0 0
19+
1 1
20+
2 2
21+
3 3
22+
4 4
23+
};
24+
\addlegendentry{scatter}
25+
\addplot [semithick, color0]
26+
table {%
27+
0 0
28+
1 1
29+
2 2
30+
3 3
31+
4 4
32+
};
33+
\addlegendentry{line}
34+
\end{axis}
35+
36+
\end{tikzpicture}

test/test_line_collection_reference.tex

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,7 @@
55
\begin{axis}[
66
colorbar,
77
colorbar style={ylabel={Line Number}},
8-
colormap={mymap}{[1pt]
9-
rgb(0pt)=(1,0,0);
10-
rgb(10pt)=(1,0.9375,0);
11-
rgb(11pt)=(0.96875,1,0);
12-
rgb(21pt)=(0.03125,1,0);
13-
rgb(22pt)=(0,1,0.0625);
14-
rgb(32pt)=(0,1,1);
15-
rgb(42pt)=(0,0.0625,1);
16-
rgb(43pt)=(0.03125,0,1);
17-
rgb(53pt)=(0.96875,0,1);
18-
rgb(54pt)=(1,0,0.9375);
19-
rgb(63pt)=(1,0,0.09375)
20-
},
8+
colormap/viridis,
219
point meta max=9,
2210
point meta min=0,
2311
tick align=outside,

test/test_noise2_reference.tex

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
\begin{axis}[
44
colorbar horizontal,
55
colorbar style={xtick={-1,0,1},xticklabels={Low,Medium,High}},
6-
colormap={mymap}{[1pt]
7-
rgb(0pt)=(1,0,0);
8-
rgb(10pt)=(1,0.9375,0);
9-
rgb(11pt)=(0.96875,1,0);
10-
rgb(21pt)=(0.03125,1,0);
11-
rgb(22pt)=(0,1,0.0625);
12-
rgb(32pt)=(0,1,1);
13-
rgb(42pt)=(0,0.0625,1);
14-
rgb(43pt)=(0.03125,0,1);
15-
rgb(53pt)=(0.96875,0,1);
16-
rgb(54pt)=(1,0,0.9375);
17-
rgb(63pt)=(1,0,0.09375)
18-
},
6+
colormap/viridis,
197
point meta max=1,
208
point meta min=-1,
219
tick align=outside,

test/test_noise_reference.tex

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
\begin{axis}[
44
colorbar,
55
colorbar style={ytick={-1,0,1},yticklabels={< -1,0,> 1},ylabel={}},
6-
colormap={mymap}{[1pt]
7-
rgb(0pt)=(1,0,0);
8-
rgb(10pt)=(1,0.9375,0);
9-
rgb(11pt)=(0.96875,1,0);
10-
rgb(21pt)=(0.03125,1,0);
11-
rgb(22pt)=(0,1,0.0625);
12-
rgb(32pt)=(0,1,1);
13-
rgb(42pt)=(0,0.0625,1);
14-
rgb(43pt)=(0.03125,0,1);
15-
rgb(53pt)=(0.96875,0,1);
16-
rgb(54pt)=(1,0,0.9375);
17-
rgb(63pt)=(1,0,0.09375)
18-
},
6+
colormap/viridis,
197
point meta max=1,
208
point meta min=-1,
219
tick align=outside,

0 commit comments

Comments
 (0)