I have a two-dimensional image plotted by Matplotlib using the PGF backend. It is stored in the figures subdirectory (since I have a lot of images and don't want to clutter up the main folder). As an example of the generation process:
from matplotlib import pyplot as plt
import numpy as np
plt.imshow(np.random.randn(200, 200))
plt.title('Some random data')
plt.savefig('figures/test.pgf')
The figures subdirectory now contains test.pgf and the raster image test-img0.png. The PGF file includes the command \pgfimage[interpolate=true,width=4.810000in,height=4.810000in]{test-img0.png}} to include the PNG. Since it (by design) does not contain the figures/ prefix, I need to find a workaround. According to a comment at a top of the PGF file (and backed up by this comment on the Matplotlib issue tracker) I can use the import package to make things work:
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{import}
\usepackage{pgf}
\begin{document}
\begin{figure}
\begin{center}
\import{figures/}{test.pgf}
\end{center}
\end{figure}
\end{document}
When I compile this (xelatex test), I get the message
Package pgf Warning: File "test-img0.png" not found when defining image "pgflastimage".
(pgf) Tried all extensions in ".pdf:.jpg:.jpeg:.png:" on input line 62.
The PGF file is found, so the resulting PDF has the axes, title etc, but just a placeholder instead of the PNG.
I also tried defining \graphicspath{{figures/}} but PGF doesn't seem to use that either.
Adding the figures directory to the TEXINPUTS environment variable prior to compiling works, but it isn't the cleanest workaround. I could also edit the PGF file to include the path, but I'd have to do that every time I regenerate the image. Can anybody suggest why import isn't working, or an alternative method?