nschloe / tikzplotlib

:bar_chart: Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError when using `tex_relative_path_to_data` option

Dorian-Boully opened this issue · comments

Using the option tex_relative_path_to_data results in the following error :

File "C:\Python 3.9\lib\site-packages\tikzplotlib\_save.py", line 260, in save
  code = get_tikz_code(*args, filepath=filepath, **kwargs)
File "C:\Python 3.9\lib\site-packages\tikzplotlib\_save.py", line 237, in get_tikz_code
  code += "".join(content)
TypeError: sequence item 5: expected str instance, WindowsPath found

Here is an example of Python code that produces the error :

import matplotlib.pyplot as plt
import numpy as np

plt.style.use("ggplot")

t = np.arange(0.0, 2.0, 0.1)
s = np.sin(2 * np.pi * t)
s2 = np.cos(2 * np.pi * t)
plt.plot(t, s, "o-", lw=4.1)
plt.plot(t, s2, "o-", lw=4.1)
plt.xlabel("time (s)")
plt.ylabel("Voltage (mV)")
plt.title("Simple plot $\\frac{\\alpha}{2}$")
plt.grid(True)

import tikzplotlib

tikzplotlib.save(
    "test/tex/test.tex",
    externalize_tables=True,
    tex_relative_path_to_data='tex/to/data/path', # <----- The line that causes the error
)

Because of the error there is no output written.
The expected tex output would be the following (with tex/to/data/path/ in the \addplot table argument) :

% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.886274509803922,0.290196078431373,0.2}
\definecolor{color1}{rgb}{0.203921568627451,0.541176470588235,0.741176470588235}

\begin{axis}[
axis background/.style={fill=white!89.8039215686275!black},
axis line style={white},
tick align=outside,
tick pos=left,
title={Simple plot \(\displaystyle \frac{\alpha}{2}\)},
x grid style={white},
xlabel={time (s)},
xmajorgrids,
xmin=-0.095, xmax=1.995,
xtick style={color=white!33.3333333333333!black},
y grid style={white},
ylabel={Voltage (mV)},
ymajorgrids,
ymin=-1.1, ymax=1.1,
ytick style={color=white!33.3333333333333!black}
]
\addplot [line width=1.64pt, color0, mark=*, mark size=3, mark options={solid}]
table {%
tex/to/data/path/test/tex/test-000.tsv};
\addplot [line width=1.64pt, color1, mark=*, mark size=3, mark options={solid}]
table {%
tex/to/data/path/test/tex/test-001.tsv};
\end{axis}

\end{tikzpicture}

When running the same code without the tex_relative_path_to_data option everything is fine :

import matplotlib.pyplot as plt
import numpy as np

plt.style.use("ggplot")

t = np.arange(0.0, 2.0, 0.1)
s = np.sin(2 * np.pi * t)
s2 = np.cos(2 * np.pi * t)
plt.plot(t, s, "o-", lw=4.1)
plt.plot(t, s2, "o-", lw=4.1)
plt.xlabel("time (s)")
plt.ylabel("Voltage (mV)")
plt.title("Simple plot $\\frac{\\alpha}{2}$")
plt.grid(True)

import tikzplotlib

tikzplotlib.save(
    "test/tex/test.tex",
    externalize_tables=True,
    # tex_relative_path_to_data='tex/to/data/path', # <----- The line that causes the error
)

gives the following result

% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.886274509803922,0.290196078431373,0.2}
\definecolor{color1}{rgb}{0.203921568627451,0.541176470588235,0.741176470588235}

\begin{axis}[
axis background/.style={fill=white!89.8039215686275!black},
axis line style={white},
tick align=outside,
tick pos=left,
title={Simple plot \(\displaystyle \frac{\alpha}{2}\)},
x grid style={white},
xlabel={time (s)},
xmajorgrids,
xmin=-0.095, xmax=1.995,
xtick style={color=white!33.3333333333333!black},
y grid style={white},
ylabel={Voltage (mV)},
ymajorgrids,
ymin=-1.1, ymax=1.1,
ytick style={color=white!33.3333333333333!black}
]
\addplot [line width=1.64pt, color0, mark=*, mark size=3, mark options={solid}]
table {%
test-000.tsv};
\addplot [line width=1.64pt, color1, mark=*, mark size=3, mark options={solid}]
table {%
test-001.tsv};
\end{axis}

\end{tikzpicture}

This time no additional path is written in the \addplot table argument.