From dd6b9eee39527de88432a9afe149b173440c3eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20Schl=C3=B6mer?= Date: Wed, 11 Aug 2021 10:24:02 +0200 Subject: [PATCH] small fixes for tables --- src/tikzplotlib/_line2d.py | 39 +++++++++++++++++++++----------------- src/tikzplotlib/_save.py | 2 +- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/tikzplotlib/_line2d.py b/src/tikzplotlib/_line2d.py index 8a3a8927..29cd5c4f 100644 --- a/src/tikzplotlib/_line2d.py +++ b/src/tikzplotlib/_line2d.py @@ -263,17 +263,6 @@ def _table(obj, data): # noqa: C901 # don't want the \n in the table definition, just in the data (below) opts.append("row sep=" + data["table_row_sep"].strip()) - if data["externalize tables"] and data["externals search path"] is not None: - esp = data["externals search path"] - opts.append(f"search path={{{esp}}}") - - if len(opts) > 0: - opts_str = ",".join(opts) - content.append(f"table [{opts_str}] {{") - else: - content.append("table {") - - plot_table = [] table_row_sep = data["table_row_sep"] ydata[ydata_mask] = np.nan if np.any(ydata_mask) or ~np.all(np.isfinite(ydata)): @@ -283,18 +272,34 @@ def _table(obj, data): # noqa: C901 if "unbounded coords=jump" not in data["current axes"].axis_options: data["current axes"].axis_options.append("unbounded coords=jump") - for x, y in zip(xdata, ydata): - plot_table.append(f"{x:{xformat}}{col_sep}{y:{ff}}{table_row_sep}") + plot_table = [ + f"{x:{xformat}}{col_sep}{y:{ff}}{table_row_sep}" for x, y in zip(xdata, ydata) + ] + + min_extern_length = 3 - if data["externalize tables"]: + if data["externalize tables"] and len(xdata) >= min_extern_length: filepath, rel_filepath = _files.new_filepath(data, "table", ".dat") with open(filepath, "w") as f: # No encoding handling required: plot_table is only ASCII f.write("".join(plot_table)) - content.append(str(rel_filepath)) + + if data["externals search path"] is not None: + esp = data["externals search path"] + opts.append(f"search path={{{esp}}}") + + if len(opts) > 0: + opts_str = ",".join(opts) + content.append(f"table [{opts_str}] {{{rel_filepath}}};\n") + else: + content.append(f"table {{{rel_filepath}}};\n") else: - content.append("%\n") + if len(opts) > 0: + opts_str = ",".join(opts) + content.append(f"table [{opts_str}] {{%\n") + else: + content.append("table {%\n") content.extend(plot_table) + content.append("};\n") - content.append("};\n") return content, axis_options diff --git a/src/tikzplotlib/_save.py b/src/tikzplotlib/_save.py index 32011406..0dc7e631 100644 --- a/src/tikzplotlib/_save.py +++ b/src/tikzplotlib/_save.py @@ -218,7 +218,7 @@ def get_tikz_code( code = """""" if include_disclaimer: - disclaimer = f"This file was created by tikzplotlib v{__version__}." + disclaimer = f"This file was created with tikzplotlib v{__version__}." code += _tex_comment(disclaimer) # write the contents