將 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

您將widthlength(生成的圖片的物理尺寸)與 x 和 y 值範圍混合。請注意,該圖形將是向量的,因此您不能在這裡使用像素,至少不那麼容易。

\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 標籤添加了空間,即使它們不存在,並且通常網格是在下面圖片。

相關內容