Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't duplicate plot instructions to tikz #610

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ dependencies = [
"webcolors",
]

[project.optional-dependencies]
dev = [
"seaborn"
]

[project.urls]
Code = "https://github.com/nschloe/tikzplotlib"
Issues = "https://github.com/nschloe/tikzplotlib/issues"
Expand Down
69 changes: 42 additions & 27 deletions src/tikzplotlib/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,25 @@ def draw_pathcollection(data, obj):
ls = None

if add_individual_color_code:
draw_options.extend(
[
"scatter",
"visualization depends on={value \\thisrow{draw} \\as \\drawcolor}",
"visualization depends on={value \\thisrow{fill} \\as \\fillcolor}",
"scatter/@pre marker code/.code={%\n"
+ " \\expanded{%\n"
+ " \\noexpand\\definecolor{thispointdrawcolor}{RGB}{\\drawcolor}%\n"
+ " \\noexpand\\definecolor{thispointfillcolor}{RGB}{\\fillcolor}%\n"
+ " }%\n"
+ " \\scope[draw=thispointdrawcolor, fill=thispointfillcolor]%\n"
+ "}",
"scatter/@post marker code/.code={%\n \\endscope\n}",
]
)
draw_options.append("scatter")
if "draw" in labels or "fill" in labels:
if "draw" in labels:
draw_options.append("visualization depends on={value \\thisrow{draw} \\as \\drawcolor}")
if "fill" in labels:
draw_options.append("visualization depends on={value \\thisrow{fill} \\as \\fillcolor}")
scope_vars = ", ".join(f"{var}=thispoint{var}color" for var in ("draw", "fill") if var in labels)
draw_options.extend(
[
"scatter/@pre marker code/.code={%\n"
+ " \\expanded{%\n"
+ (" \\noexpand\\definecolor{thispointdrawcolor}{RGB}{\\drawcolor}%\n" if "draw" in labels else "")
+ (" \\noexpand\\definecolor{thispointfillcolor}{RGB}{\\fillcolor}%\n" if "fill" in labels else "")
+ " }%\n"
+ " \\scope[" + scope_vars + "]%\n"
+ "}",
"scatter/@post marker code/.code={%\n \\endscope\n}",
]
)

# "solution" from
# <https://github.com/matplotlib/matplotlib/issues/4672#issuecomment-378702670>
Expand Down Expand Up @@ -249,7 +253,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 +297,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{{{legend_text}}}\n")
Expand Down
18 changes: 18 additions & 0 deletions tests/test_seaborn_scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def plot():
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns

np.random.seed(123)
data = np.random.rand(4, 64)

fig, ax = plt.subplots()
sns.scatterplot({str(i): d for i, d in enumerate(data)}, ax=ax)

return fig


def test():
from .helpers import assert_equality

assert_equality(plot, __file__[:-3] + "_reference.tex")
Loading