diff --git a/src/tikzplotlib/_path.py b/src/tikzplotlib/_path.py index 733425d2..65be8b24 100644 --- a/src/tikzplotlib/_path.py +++ b/src/tikzplotlib/_path.py @@ -249,7 +249,11 @@ def draw_pathcollection(data, obj): if legend_text is None and has_legend(obj.axes): draw_options.append("forget plot") + # track content that's been seen so we can de-duplicate (this is a bit of a kludge) + code_seen = set() + table_map = {} for path in obj.get_paths(): + cur_content = [] if is_contour: dd = path.vertices # https://matplotlib.org/stable/api/path_api.html @@ -289,7 +293,7 @@ def draw_pathcollection(data, obj): len_row = sum(len(item) for item in draw_options) j0, j1, j2 = ("", ", ", "") if len_row < 80 else ("\n ", ",\n ", "\n") do = f" [{j0}{{}}{j2}]".format(j1.join(draw_options)) if draw_options else "" - content.append(f"\\addplot{do}\n") + cur_content.append(f"\\addplot{do}\n") if data["externals search path"] is not None: esp = data["externals search path"] @@ -297,9 +301,9 @@ def draw_pathcollection(data, obj): if len(table_options) > 0: table_options_str = ", ".join(table_options) - content.append(f"table [{table_options_str}]{{") + cur_content.append(f"table [{table_options_str}]{{") else: - content.append("table{") + cur_content.append("table{") plot_table = [] plot_table.append(" ".join(labels) + "\n") @@ -307,16 +311,23 @@ def draw_pathcollection(data, obj): plot_table.append(" ".join(row) + "\n") if data["externalize tables"]: - 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)) + plot_table_str = "".join(plot_table) + if plot_table_str not in table_map: + 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)) + table_map[plot_table_str] = str(rel_filepath) + cur_content.append(table_map[plot_table_str]) else: - content.append("%\n") - content.extend(plot_table) - - content.append("};\n") + cur_content.append("%\n") + cur_content.extend(plot_table) + + cur_content.append("};\n") + # very dumb kludge: skip duplicates + if tuple(cur_content) not in code_seen: + content.extend(cur_content) + code_seen.add(tuple(cur_content)) if legend_text is not None: content.append(f"\\addlegendentry{{{_common_texification(legend_text)}}}\n")