Zeichnen Sie pgfplots-Gitter auf ein Bild

Zeichnen Sie pgfplots-Gitter auf ein Bild

Ich möchte ein pgfplots-Raster auf ein 480 x 800 großes Bild zeichnen. Mit dem folgenden Code kann ich sehen, dass das Raster gezeichnet wurde, aber das Bild wird nicht angezeigt.

\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}

Ausgabe: Bildbeschreibung hier eingeben

Antwort1

Sie vermischen widthund length(physische Größe des erzeugten Bildes) mit den x- und y-Wertebereichen. Beachten Sie, dass die Abbildung vektoriell ist, Sie können hier also nicht mit Pixeln spielen, zumindest nicht so einfach.

\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}

Bildbeschreibung hier eingeben

Beachten Sie, dass pgfplots Platz für x- und y-Beschriftungen hinzufügt, auch wenn diese nicht vorhanden sind, und dass das Raster normalerweiseunterdas Bild.

verwandte Informationen