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

tex_relative_path_to_data makes slashes in Windows style, which is not accepted by LaTeX.

IsabellLehmann opened this issue · comments

Hi,

When I give an argument to tex_relative_path_to_data, the slashes in the .tex-file are in Windows style but LaTeX does not accept them. Would be good if they are these '/' also for Windows PCs.

Furthermore, I have no idea why there is images/images in the path. I think it should be there only once.

Here you find:

  • the Python code (make sure a folder images exists in the folder where you run the code),
import numpy as np
import matplotlib.pyplot as plt
import tikzplotlib

plt.imshow(np.random.randn(20,20))
tikzplotlib.save('images/test.tex', tex_relative_path_to_data='images', override_externals=True)
  • the output TeX (Pgfplots), and
% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=19.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.0196078431373!black},
ymin=-0.5, ymax=19.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=19.5, ymin=19.5, ymax=-0.5] {images\images\test-000.png};
\end{axis}

\end{tikzpicture}
  • the expected TeX (Pgfplots).
% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=19.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.0196078431373!black},
ymin=-0.5, ymax=19.5,
ytick style={color=black}
]
% the next line has the difference
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=19.5, ymin=19.5, ymax=-0.5] {images/test-000.png};
\end{axis}

\end{tikzpicture}

Thanks for any suggestions besides manually adjusting the path in each image!
Isabell

I am also not entirely sure of how this is supposed to behave. It seems like the option tex_relative_path_to_data will be just prepended to all paths used in the tikz file. So my workaround on Windows is to use tex_relative_path_to_data for the ./plots/ directory within my tex directory and another directory ./plots/data/ within, containing all additional data, images, tables etc.

import numpy as np
import matplotlib.pyplot as plt
import tikzplotlib

plt.imshow(np.random.randn(20,20))
tikzplotlib.save('./data/test.tex', tex_relative_path_to_data='plots', override_externals=True)

Doing so, will save all files to a sub directory called ./data/ and you will have to manually move the files to the corresponding tex directories (./plots/ for tex file and ./plots/data/ for all others). This is still cumbersome but I'd rather move some files manually / via scripts than edit all auto-generated tikz files.

Regarding the issue with slashes, I cannot replicate the same behaviour, for me the above code yields correctly:

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

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=19.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.0196078431373!black},
ymin=-0.5, ymax=19.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=19.5, ymin=19.5, ymax=-0.5] {plots/data/test-000.png};
\end{axis}

\end{tikzpicture}

Thanks @Blubbaa ! Will try it out next time I need to generate tikz images :)

Strange that this error does not occur with you ... Your code generates for me:

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

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=19.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.0196078431373!black},
ymin=-0.5, ymax=19.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=19.5, ymin=19.5, ymax=-0.5] {plots\data\test-000.png};
\end{axis}

\end{tikzpicture}

Maybe I have configured my Python wrongly. Both running the code in a Jupyter notebook and in the PyCharm console results in the above...

Ok, I did some investigations, it seems I encountered this issue before and just couldn't remember 😄

There was an open PR (#473) some time ago, which proposed to fix the problem here:

if data["rel data path"]:
rel_filepath = pathlib.Path(data["rel data path"]) / filepath
else:
rel_filepath = filepath.name
return filepath, rel_filepath

by adding some string replacement for Windows paths. Back then I have manually edited the local source from tikzplotlib and added this line in L42:

    # FIX: for windows relative paths (see https://github.com/nschloe/tikzplotlib/pull/473)
    rel_filepath = str(rel_filepath).replace('\\', '/')

It seems like that this should have been fixed already, but apparently is not.

I'll look at it later.

I believe the support for .dat files was forgotten in the fix. Instead of rel_filepath here

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")

posix_filepath = rel_filepath.as_posix() should also be used instead. I am not sure if there are any other similar issues with different styles.

Don't know if I should open a new issue or just reuse the old one.

@Blubbaa Thanks for the notification. Fixed in #533.