3

I want pgfplot add a grid with absolute coordinate automatically, for example, below image original pdf is 1.17x1.19in, so I want x is (0,1.17in) and y is (0,1.19in).

Now I hardcode it to Nx/Ny, can we do it by detect the image file automatically, then I no need to change it if image changed.

After update according to reply, it works fine now.

The MWE now is:

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}

\begin{document}

\def\imgfilename{tiger}
\def\u{72.26999} % 28.4526 -> pt to cm; 72.26999 -> pt to inch
\newbox\tempfig \newlength\tempwidth \newlength\tempheight
\setbox\tempfig=\hbox{{\includegraphics{\imgfilename}}}
\tempwidth\wd\tempfig \tempheight\ht\tempfig

\pgfmathparse{\tempwidth/\u}  \pgfmathsetmacro\Nx{\pgfmathresult}
\pgfmathparse{\tempheight/\u} \pgfmathsetmacro\Ny{\pgfmathresult}

\begin{tikzpicture}
    \begin{axis}[
            grid=both,minor tick num=2,major grid style={gray!80},
            width=10cm,
            scale only axis,
            enlargelimits=false,
        axis on top]
        \addplot graphics [xmin=0,xmax=\Nx,ymin=0,ymax=\Ny,
            includegraphics={},
        ] {\imgfilename};
    \end{axis}
    \draw(current bounding box.north east)node[above left]{$(\Nx\times\Ny\ in)$};
\end{tikzpicture}

\end{document}

The output is:

cute tiger

Original tiger.png is:

cute tiger

1

See How do I get the exact dimension of a picture in LaTeX? and Getting length as number? for more details.

\documentclass{article}
    \usepackage{tikz,pgfplots}
    \usetikzlibrary{external}\tikzexternalize
\begin{document}
    \foreach\n in{0,...,4}{
        \tikz\draw[ball color=white,rotate=30](0,0)rectangle(1+\n,2+\n*\n);
    }
    \newbox\tempfig \newlength\tempwidth \newlength\tempheight
    \foreach\n in{0,...,4}{
        \setbox\tempfig\hbox{\includegraphics{\jobname-figure\n.pdf}}
        \tempwidth\wd\tempfig \tempheight\ht\tempfig
        \tikzset{external/export next=false}
        \begin{tikzpicture}
            \pgfmathsetmacro\Nx{\tempwidth} \pgfmathsetmacro\Ny{\tempheight}
            \begin{axis}[grid=both,enlargelimits=false,axis on top]
                \addplot graphics[xmin=0,xmax=\Nx,ymin=0,ymax=\Ny]{\jobname-figure\n.pdf};
            \end{axis}
            \draw(current bounding box.north east)node[above left]{$(\Nx\times\Ny)$};
        \end{tikzpicture}
    }
\end{document}
| improve this answer | |
  • Thanks, after change code according to your example, \Nx seems fine but \Ny is 7.5. what's the unit of the value? – Beatlej Jan 7 '15 at 18:08
  • Should be pt. You can write \pgfmathsetmacro\Ny{\tempheight*72.27} to convert it. – Symbol 1 Jan 8 '15 at 0:33
  • Thanks, it works fine now as updated in question. root cause of the Ny issue is "\usetikzlibrary{external}\tikzexternalize", after remove this line, it works fine. What's the purpose of this line? why it doesn't work on MacOSX? – Beatlej Jan 8 '15 at 3:21

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.