Skip to content

Commit e4bf54b

Browse files
authored
Merge branch 'master' into dashed_lines
2 parents 2e111d1 + ca8c799 commit e4bf54b

File tree

6 files changed

+86
-55
lines changed

6 files changed

+86
-55
lines changed

matplotlib2tikz/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
__email__ = "[email protected]"
55
__copyright__ = u"Copyright (c) 2010-2019, {} <{}>".format(__author__, __email__)
66
__license__ = "License :: OSI Approved :: MIT License"
7-
__version__ = "0.7.3"
7+
__version__ = "0.7.4"
88
__status__ = "Development Status :: 5 - Production/Stable"

matplotlib2tikz/line2d.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def draw_line2d(data, obj):
2121
content = []
2222
addplot_options = []
2323

24-
# If line is of length 0, do nothing. Otherwise, an empty \addplot table
25-
# will be created, which will be interpreted as an external data source
26-
# in either the file '' or '.tex'. Instead, render nothing.
24+
# If line is of length 0, do nothing. Otherwise, an empty \addplot table will be
25+
# created, which will be interpreted as an external data source in either the file
26+
# '' or '.tex'. Instead, render nothing.
2727
if len(obj.get_xdata()) == 0:
2828
return data, []
2929

@@ -291,7 +291,10 @@ def _table(obj, data):
291291
xdata = [date.strftime("%Y-%m-%d %H:%M") for date in xdata]
292292
xformat = "{}"
293293
col_sep = ","
294-
content.append("table [header=false,col sep=comma] {%\n")
294+
opts = ["header=false", "col sep=comma"]
295+
if data["table_row_sep"] != "\n":
296+
opts.append("row sep=" + data["table row sep"])
297+
content.append("table [{}] {{%\n".format(",".join(opts)))
295298
data["current axes"].axis_options.append("date coordinates in=x")
296299
# Replace float xmin/xmax by datetime
297300
# <https://github.com/matplotlib/matplotlib/issues/13727>.
@@ -321,12 +324,18 @@ def _table(obj, data):
321324

322325
for (x, y, is_masked) in zip(xdata, ydata, ydata_mask):
323326
if is_masked:
324-
plot_table.append((xformat + col_sep + "nan\n").format(x))
327+
plot_table.append(
328+
(xformat + col_sep + "nan" + data["table_row_sep"]).format(x)
329+
)
325330
else:
326-
plot_table.append((xformat + col_sep + ff + "\n").format(x, y))
331+
plot_table.append(
332+
(xformat + col_sep + ff + data["table_row_sep"]).format(x, y)
333+
)
327334
else:
328335
for x, y in zip(xdata, ydata):
329-
plot_table.append((xformat + col_sep + ff + "\n").format(x, y))
336+
plot_table.append(
337+
(xformat + col_sep + ff + data["table_row_sep"]).format(x, y)
338+
)
330339

331340
if data["externalize tables"]:
332341
filename, rel_filepath = files.new_filename(data, "table", ".tsv")

matplotlib2tikz/save.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def get_tikz_code(
4242
include_disclaimer=True,
4343
standalone=False,
4444
float_format="{:.15g}",
45+
table_row_sep="\n",
4546
):
4647
"""Main function. Here, the recursion into the image starts and the
4748
contents are picked up. The actual file gets written in this routine.
@@ -112,7 +113,7 @@ def get_tikz_code(
112113
:type extra_axis_parameters: a list or set of strings for the pfgplots axes.
113114
114115
:param extra_tikzpicture_parameters: Extra tikzpicture options to be passed
115-
(as a set) to pgfplots.
116+
(as a set) to pgfplots.
116117
117118
:type extra_tikzpicture_parameters: a set of strings for the pfgplots
118119
tikzpicture.
@@ -122,8 +123,24 @@ def get_tikz_code(
122123
``savefig.dpi`` from matplotlib.rcParams. Default is ``None``.
123124
:type dpi: int
124125
125-
:returns: None
126+
:param show_info: Show extra info on the command line. Default is ``False``.
127+
:type show_info: bool
128+
129+
:param include_disclaimer: Include matplotlib2tikz disclaimer in the output.
130+
Set ``False`` to make tests reproducible.
131+
Default is ``True``.
132+
:type include_disclaimer: bool
133+
134+
:param standalone: Include wrapper code for a standalone LaTeX file.
135+
:type standalone: bool
126136
137+
:param float_format: Format for float entities. Default is ```"{:.15g}"```.
138+
:type float_format: str
139+
140+
:param table_row_sep: Row separator for table data. Default is ```"\\n"```.
141+
:type table_row_sep: str
142+
143+
:returns: None
127144
128145
The following optional attributes of matplotlib's objects are recognized
129146
and handled:
@@ -179,6 +196,7 @@ def get_tikz_code(
179196
)
180197

181198
data["float format"] = float_format
199+
data["table_row_sep"] = table_row_sep
182200

183201
# print message about necessary pgfplot libs to command line
184202
if show_info:
@@ -245,7 +263,7 @@ def save(filepath, *args, encoding=None, **kwargs):
245263
For supported values: see ``codecs`` module.
246264
:returns: None
247265
"""
248-
code = get_tikz_code(*args, **kwargs)
266+
code = get_tikz_code(*args, filepath=filepath, **kwargs)
249267
file_handle = codecs.open(filepath, "w", encoding)
250268
try:
251269
file_handle.write(code)

test/helpers.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,21 @@ def _unidiff_output(expected, actual):
3333
return "".join(diff)
3434

3535

36-
def assert_equality(plot, filename):
36+
def assert_equality(plot, filename, **extra_get_tikz_code_args):
3737
plot()
38-
code = matplotlib2tikz.get_tikz_code(include_disclaimer=False)
38+
code = matplotlib2tikz.get_tikz_code(
39+
include_disclaimer=False, **extra_get_tikz_code_args
40+
)
3941
plt.close()
4042

4143
this_dir = os.path.dirname(os.path.abspath(__file__))
4244
with open(os.path.join(this_dir, filename), "r", encoding="utf-8") as f:
4345
reference = f.read()
4446
assert reference == code, _unidiff_output(reference, code)
4547

46-
code = matplotlib2tikz.get_tikz_code(include_disclaimer=False, standalone=True)
48+
code = matplotlib2tikz.get_tikz_code(
49+
include_disclaimer=False, standalone=True, **extra_get_tikz_code_args
50+
)
4751
assert _compile(code) is not None
4852
return
4953

test/test_basic_sin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def plot():
2525

2626

2727
def test():
28-
assert_equality(plot, "test_basic_sin_reference.tex")
28+
assert_equality(plot, "test_basic_sin_reference.tex", table_row_sep="\\\\\n")
2929
return
3030

3131

test/test_basic_sin_reference.tex

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,50 @@
2424
]
2525
\addplot [semithick, color0, mark=*, mark size=3, mark options={solid}]
2626
table {%
27-
0 0
28-
0.1 0.587785252292473
29-
0.2 0.951056516295154
30-
0.3 0.951056516295154
31-
0.4 0.587785252292473
32-
0.5 1.22464679914735e-16
33-
0.6 -0.587785252292473
34-
0.7 -0.951056516295154
35-
0.8 -0.951056516295154
36-
0.9 -0.587785252292473
37-
1 -2.44929359829471e-16
38-
1.1 0.587785252292474
39-
1.2 0.951056516295154
40-
1.3 0.951056516295154
41-
1.4 0.587785252292473
42-
1.5 3.67394039744206e-16
43-
1.6 -0.587785252292473
44-
1.7 -0.951056516295154
45-
1.8 -0.951056516295154
46-
1.9 -0.587785252292473
27+
0 0\\
28+
0.1 0.587785252292473\\
29+
0.2 0.951056516295154\\
30+
0.3 0.951056516295154\\
31+
0.4 0.587785252292473\\
32+
0.5 1.22464679914735e-16\\
33+
0.6 -0.587785252292473\\
34+
0.7 -0.951056516295154\\
35+
0.8 -0.951056516295154\\
36+
0.9 -0.587785252292473\\
37+
1 -2.44929359829471e-16\\
38+
1.1 0.587785252292474\\
39+
1.2 0.951056516295154\\
40+
1.3 0.951056516295154\\
41+
1.4 0.587785252292473\\
42+
1.5 3.67394039744206e-16\\
43+
1.6 -0.587785252292473\\
44+
1.7 -0.951056516295154\\
45+
1.8 -0.951056516295154\\
46+
1.9 -0.587785252292473\\
4747
};
4848
\addlegendentry{sin}
4949
\addplot [very thick, color1, opacity=0.3, mark=*, mark size=3, mark options={solid}]
5050
table {%
51-
0 1
52-
0.1 0.809016994374947
53-
0.2 0.309016994374947
54-
0.3 -0.309016994374948
55-
0.4 -0.809016994374947
56-
0.5 -1
57-
0.6 -0.809016994374947
58-
0.7 -0.309016994374948
59-
0.8 0.309016994374947
60-
0.9 0.809016994374947
61-
1 1
62-
1.1 0.809016994374947
63-
1.2 0.309016994374947
64-
1.3 -0.309016994374947
65-
1.4 -0.809016994374947
66-
1.5 -1
67-
1.6 -0.809016994374948
68-
1.7 -0.309016994374946
69-
1.8 0.309016994374947
70-
1.9 0.809016994374947
51+
0 1\\
52+
0.1 0.809016994374947\\
53+
0.2 0.309016994374947\\
54+
0.3 -0.309016994374948\\
55+
0.4 -0.809016994374947\\
56+
0.5 -1\\
57+
0.6 -0.809016994374947\\
58+
0.7 -0.309016994374948\\
59+
0.8 0.309016994374947\\
60+
0.9 0.809016994374947\\
61+
1 1\\
62+
1.1 0.809016994374947\\
63+
1.2 0.309016994374947\\
64+
1.3 -0.309016994374947\\
65+
1.4 -0.809016994374947\\
66+
1.5 -1\\
67+
1.6 -0.809016994374948\\
68+
1.7 -0.309016994374946\\
69+
1.8 0.309016994374947\\
70+
1.9 0.809016994374947\\
7171
};
7272
\addlegendentry{cos}
7373
\end{axis}

0 commit comments

Comments
 (0)