画像にpgfplotsグリッドを描く

画像にpgfplotsグリッドを描く

480x800 の画像に pgfplots グリッドを描画したいと思います。以下のコードでは、グリッドがプロットされていることはわかりますが、画像は表示されません。

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.18}
\begin{document}
\def\fname{/media/sf_work/demo.png}
\begin{tikzpicture}
\begin{axis}[
        grid = both,scale=.5,
        width=480,
        height=800,
        , minor tick num=3
        , grid style={draw=gray!10,line width=.1pt}
        , major grid style={line width=0.5pt,draw=gray!50}
        , axis line style={latex-latex}
        , xticklabels = \empty
        , yticklabels = \empty
        , draw=blue!20
    ]
  \node[] (image) at (axis cs:240,400) {\includegraphics[]{\fname}};
\end{axis}
\end{tikzpicture} 
\end{document}

出力: ここに画像の説明を入力してください

答え1

widthand (生成される画像の物理的なサイズ) と x および y 値の範囲を混在させていますlength。図はベクトルなので、ここではピクセルを操作することはできません (少なくとも簡単には)。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\def\fname{/media/sf_work/demo.png}
\begin{tikzpicture}
\begin{axis}[
    grid = both, %scale removed, here it messes things up
        width=4.8cm,
        height=8cm,
        xmin=0, xmax=480, ymin=0, ymax=800,
        , minor tick num=3
        , grid style={draw=gray!10,line width=.1pt}
        , major grid style={line width=0.5pt,draw=gray!50}
        , axis line style={latex-latex}
        , xticklabels = \empty
        , yticklabels = \empty
        , draw=blue!20
    ]
    % axis cs is not needed
  \node[] (image) at (axis cs:240,400) {\includegraphics[width=2cm]{example-image-duck}};
\end{axis}
\end{tikzpicture}
\end{document}

ここに画像の説明を入力してください

pgfplotsはxとyのラベルがなくてもスペースを追加し、通常はグリッドは絵。

関連情報