nschloe / tikzplotlib

:bar_chart: Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.
MIT License
2.42k stars 218 forks source link

Relative path between TeX and image outputs #553

Open qwenger opened 2 years ago

qwenger commented 2 years ago

I have the following structure:

├── images
│   ├── plot-000.png
│   └── plot.tex
├── main.tex
├── Makefile
└── plot.py

main.tex:

\documentclass{article}

%\usepackage[luatex]{graphicx}
%\graphicspath{{images/}}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.16}

\begin{document}
    \input{images/plot.tex}
\end{document}

Makefile:

all:
    python plot.py
    lualatex main.tex

plot.py:

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

x = np.linspace(0.0, 1.0, 500)
y = np.linspace(0.0, 1.0, 500)
x_2d, y_2d = np.meshgrid(x, y)

step = np.where((0.25 <= x_2d) & (x_2d <= 0.75) & (0.25 <= y_2d) & (y_2d <= 0.75), 1.0, 0.0)

plt.imshow(step)

tikzplotlib.save(
    "images/plot.tex",
#    tex_relative_path_to_data="images/",
#    tex_relative_path_to_data=os.path.abspath("images/"),
)

generated plot.tex:

% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}

\definecolor{darkgray176}{RGB}{176,176,176}

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

\end{tikzpicture}

I would like generated *.tex files to be stored in images/ together with their *.png companions.

However, if I do so (with the images/ prefix in tikzplotlib.save), when compiling main.tex I get:

Package pgf Warning: File "plot-000.png" not found when defining image "pgflast
image". Tried all extensions in ".pdf:.jpg:.jpeg:.png:" on input line 18.

because in plot.tex the *.png is included without path.

  1. Adding tex_relative_path_to_data="images/" to tikzplotlib.save does not work, I get the error
    FileNotFoundError: [Errno 2] No such file or directory: 'images/images/plot-000.png'
  2. Adding \graphicspath{{images/}} to main.tex does not work, because for some reason tikzplotlib forces the includegraphics cmd to \pgfimage (which does not seem to respect \graphicspath) rather than \includegraphics.
  3. There's an option table/search path in PGF for \addplot table, but there doesn't seem to be an equivalent for \addplot graphics.
  4. I can make it work with tex_relative_path_to_data=os.path.abspath("images/") in tikzplotlib.save, but that's ugly and non-portable.

Is there a good way to make this work?

qwenger commented 2 years ago

Note: I could generate plot.tex to the top-level directory, but that's seems less logical conceptually, plus it's less convenient e.g. for VCS integration.

A couple independent ideas in case tikzplotlib has to be modified for this: