Skip to content

Commit b02c60a

Browse files
authored
Merge pull request #501 from nschloe/more-externalize
More externalize
2 parents a48d84b + c3327f4 commit b02c60a

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = tikzplotlib
3-
version = 0.9.10
3+
version = 0.9.11
44
author = Nico Schlömer
55
author_email = [email protected]
66
description = Convert matplotlib figures into TikZ/PGFPlots

src/tikzplotlib/_line2d.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,12 @@ def _table(obj, data): # noqa: C901
201201

202202
if isinstance(xdata_alt[0], datetime.datetime):
203203
xdata = xdata_alt
204-
elif isinstance(xdata_alt[0], str):
205-
data["current axes"].axis_options += [
206-
"xtick={{{}}}".format(",".join([f"{x:{ff}}" for x in xdata])),
207-
"xticklabels={{{}}}".format(",".join(xdata_alt)),
208-
]
209-
xdata, ydata = transform_to_data_coordinates(obj, xdata, ydata)
210204
else:
205+
if isinstance(xdata_alt[0], str):
206+
data["current axes"].axis_options += [
207+
"xtick={{{}}}".format(",".join([f"{x:{ff}}" for x in xdata])),
208+
"xticklabels={{{}}}".format(",".join(xdata_alt)),
209+
]
211210
xdata, ydata = transform_to_data_coordinates(obj, xdata, ydata)
212211

213212
# matplotlib allows plotting of data containing `astropy.units`, but they will break
@@ -252,12 +251,9 @@ def _table(obj, data): # noqa: C901
252251
if not option.startswith("xmin")
253252
]
254253
xmin, xmax = data["current mpl axes obj"].get_xlim()
255-
data["current axes"].axis_options.append(
256-
"xmin={}, xmax={}".format(
257-
num2date(xmin).strftime("%Y-%m-%d %H:%M"),
258-
num2date(xmax).strftime("%Y-%m-%d %H:%M"),
259-
)
260-
)
254+
mindate = num2date(xmin).strftime("%Y-%m-%d %H:%M")
255+
maxdate = num2date(xmax).strftime("%Y-%m-%d %H:%M")
256+
data["current axes"].axis_options.append(f"xmin={mindate}, xmax={maxdate}")
261257
else:
262258
opts = []
263259
xformat = ff
@@ -267,7 +263,7 @@ def _table(obj, data): # noqa: C901
267263
# don't want the \n in the table definition, just in the data (below)
268264
opts.append("row sep=" + data["table_row_sep"].strip())
269265

270-
if data["externals search path"] is not None:
266+
if data["externalize tables"] and data["externals search path"] is not None:
271267
esp = data["externals search path"]
272268
opts.append(f"search path={{{esp}}}")
273269

@@ -280,9 +276,10 @@ def _table(obj, data): # noqa: C901
280276
plot_table = []
281277
table_row_sep = data["table_row_sep"]
282278
ydata[ydata_mask] = np.nan
283-
if any(ydata_mask) or (~np.isfinite(ydata)).any():
284-
# matplotlib jumps at masked or nan values, while PGFPlots by default interpolates.
285-
# Hence, if we have a masked plot, make sure that PGFPlots jumps as well.
279+
if np.any(ydata_mask) or ~np.all(np.isfinite(ydata)):
280+
# matplotlib jumps at masked or nan values, while PGFPlots by default
281+
# interpolates. Hence, if we have a masked plot, make sure that PGFPlots jumps
282+
# as well.
286283
if "unbounded coords=jump" not in data["current axes"].axis_options:
287284
data["current axes"].axis_options.append("unbounded coords=jump")
288285

src/tikzplotlib/_path.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from matplotlib.dates import DateConverter, num2date
44
from matplotlib.markers import MarkerStyle
55

6-
from . import _color
6+
from . import _color, _files
77
from ._axes import _mpl_cmap2pgf_cmap
88
from ._hatches import _mpl_hatch2pgfp_pattern
99
from ._markers import _mpl_marker2pgfp_marker
@@ -291,13 +291,31 @@ def draw_pathcollection(data, obj):
291291
do = f" [{j0}{{}}{j2}]".format(j1.join(draw_options)) if draw_options else ""
292292
content.append(f"\\addplot{do}\n")
293293

294-
to = " [{}]".format(", ".join(table_options)) if table_options else ""
295-
content.append(f"table{to}{{%\n")
294+
if data["externals search path"] is not None:
295+
esp = data["externals search path"]
296+
table_options.append(f"search path={{{esp}}}")
296297

297-
content.append(" ".join(labels) + "\n")
298+
if len(table_options) > 0:
299+
table_options_str = ", ".join(table_options)
300+
content.append(f"table [{table_options_str}]{{")
301+
else:
302+
content.append("table{")
298303

304+
plot_table = []
305+
plot_table.append(" ".join(labels) + "\n")
299306
for row in dd_strings:
300-
content.append(" ".join(row) + "\n")
307+
plot_table.append(" ".join(row) + "\n")
308+
309+
if data["externalize tables"]:
310+
filepath, rel_filepath = _files.new_filepath(data, "table", ".dat")
311+
with open(filepath, "w") as f:
312+
# No encoding handling required: plot_table is only ASCII
313+
f.write("".join(plot_table))
314+
content.append(str(rel_filepath))
315+
else:
316+
content.append("%\n")
317+
content.extend(plot_table)
318+
301319
content.append("};\n")
302320

303321
if legend_text is not None:

0 commit comments

Comments
 (0)