4

I am trying to load an eps file generated with an external software into a TikZ picture and draw a coordinate system around it, such that I can superimpose additional data.

This post (Drawing on an image with TikZ) was extremely useful, but I'm still struggling to adjust the code for my purposes.

I prepared a small example to illustrate the problem. The file b.eps contains a plot of the function f(x)=x over the interval [-5:5].

b.eps

I then try to plot the same function in TikZ such that the two curves overlap.

\documentclass{standalone}
\usepackage{pgfplots,tikz}
\begin{document}
\begin{tikzpicture}
  \node[anchor=south west,inner sep=0] (image) at (0,0)
          {\includegraphics[width=8cm]{b.eps}};
  \begin{scope}[shift={(image.south west)},%
                    x={(image.south east)},%
                    y={(image.north west)}]
     \begin{axis}[xmin=-5, xmax=5, %
                  ymin=-5, ymax=5, %
                  xlabel = $x$, ylabel = $y$] %
      \draw[green, dashed, thick]  (axis cs: -5,-5) -- (axis cs: 5,5);
     \end{axis}
  \end{scope}
\end{tikzpicture}
\end{document}

The problem seems to be that the axis-environment does not have the correct width and height, which is why the blue line and the dashed green line don't overlap.

I tried to extract the width and height of the scope and pass it on to the arguments of the axis environment, but all my attempts failed. Any advice on how to achieve that would be highly appreciated.

PDF file generated by the code above.

  • Welcome to TeX - LaTeX! The section on scaling options in the pgfplots manual is useful here. I think you want to set the axes equal option to true; it is false by default. – Andrew Swann Aug 24 '16 at 10:33
  • Hi Andrew. As suggested, I tried using the options axes equal, axes equal image, unit vector ratio = 1 1, and unit vector ratio* = 1 1. (I think the last to options are equivalent to the first two.) It didn't resolve the issue. The size mismatch between the plot and the background picture does not look dramatic for the example I showed, but if I change the width in includegraphics to say 12cm it is much more pronounced and clearly visible. – pita Aug 24 '16 at 11:02
  • 1
    Instead of using a node for the image, it would be better if you used \addplot graphics. See tex.stackexchange.com/questions/38632/image-with-axis – Jake Aug 24 '16 at 11:02
  • Note that the image b.eps that you posted doesn't run from -5 to 5, but something like -5.2 to 5.2 in x and -5.6 to 5.6 in y. In order to properly register the image, you need to figure out the exact limits when you're generating the image. – Jake Aug 24 '16 at 11:36
  • Hi Jake! Thank you very much for both your comments. \addplot graphics is exactly what I was looking for. The problem is resolved now! – pita Aug 24 '16 at 12:26
4

Following @jake hint with some approximation, just try little modification for xmin,xmax,ymin,ymax to get a good match

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false, axis on top, axis equal image]
\addplot graphics [xmin=-5.1,xmax=5.1,ymin=-5.6,ymax=5.5] {b};
\draw[green, dashed, thick]  (axis cs: -5,-5) -- (axis cs: 5,5);
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks Salim & Jake, that's exactly what I was looking for. – pita Aug 24 '16 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.