Skip to content

Commit

Permalink
Kludge: don't duplicate plot instructions to tikz
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGross committed Apr 30, 2024
1 parent de96cd9 commit 09139be
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/tikzplotlib/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -289,34 +293,41 @@ 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"]
table_options.append(f"search path={{{esp}}}")

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")
for row in dd_strings:
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")
Expand Down

0 comments on commit 09139be

Please sign in to comment.