Skip to content

Commit eb1519a

Browse files
committed
protect against int/float in xdata
Fixes #339.
1 parent 4406b3b commit eb1519a

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

test/refresh_reference_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import matplotlib.pyplot as plt
66

7-
import tikzplotlib as m2t
7+
import tikzplotlib as tpl
88

99

1010
def _main():
@@ -24,7 +24,7 @@ def _main():
2424
spec.loader.exec_module(module)
2525
module.plot()
2626

27-
code = m2t.get_tikz_code(include_disclaimer=False)
27+
code = tpl.get_tikz_code(include_disclaimer=False)
2828
plt.close()
2929

3030
tex_filename = filename[:-3] + "_reference.tex"

test/test_line_set_data.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# from <https://github.com/nschloe/tikzplotlib/issues/339>
2+
import matplotlib.pyplot as plt
3+
4+
from helpers import assert_equality
5+
6+
7+
def plot():
8+
fig = plt.figure()
9+
line = plt.plot(0, 0, "kx")[0]
10+
line.set_data(0, 0)
11+
return fig
12+
13+
14+
def test():
15+
assert_equality(plot, "test_line_set_data_reference.tex")
16+
return

test/test_line_set_data_reference.tex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
\begin{tikzpicture}
2+
3+
\begin{axis}[
4+
tick align=outside,
5+
tick pos=left,
6+
x grid style={white!69.01960784313725!black},
7+
xmin=-0.055, xmax=0.055,
8+
xtick style={color=black},
9+
y grid style={white!69.01960784313725!black},
10+
ymin=-0.055, ymax=0.055,
11+
ytick style={color=black}
12+
]
13+
\addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks]
14+
table {%
15+
0 0
16+
};
17+
\end{axis}
18+
19+
\end{tikzpicture}

tikzplotlib/line2d.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ def draw_line2d(data, obj):
1818
# If line is of length 0, do nothing. Otherwise, an empty \addplot table will be
1919
# created, which will be interpreted as an external data source in either the file
2020
# '' or '.tex'. Instead, render nothing.
21-
if len(obj.get_xdata()) == 0:
21+
xdata = obj.get_xdata()
22+
if isinstance(xdata, int) or isinstance(xdata, float):
23+
# https://github.com/nschloe/tikzplotlib/issues/339
24+
xdata = [xdata]
25+
26+
if len(xdata) == 0:
2227
return data, []
2328

2429
# get the linewidth (in pt)
@@ -250,12 +255,15 @@ def _marker(
250255
return
251256

252257

253-
def _table(obj, data):
258+
def _table(obj, data): # noqa: C901
254259
# get_xydata() always gives float data, no matter what
255260
xdata, ydata = obj.get_xydata().T
256261

257262
# get_{x,y}data gives datetime or string objects if so specified in the plotter
258263
xdata_alt = obj.get_xdata()
264+
if isinstance(xdata_alt, int) or isinstance(xdata, float):
265+
# https://github.com/nschloe/tikzplotlib/issues/339
266+
xdata_alt = [xdata_alt]
259267

260268
ff = data["float format"]
261269

0 commit comments

Comments
 (0)