如何将图形精确地拟合到 pgfplots 中的轴上?

如何将图形精确地拟合到 pgfplots 中的轴上?

我有一个 png 和一个我想放在其上的轴。包含的 png 需要与轴完全对齐,我该怎么做?

这是我目前的代码:

\begin{tikzpicture}
  \node{\includegraphics[width=0.45\textwidth,height=0.3\textwidth]{graphics/foo.png}};
  \begin{axis}[
    width=0.45\textwidth,
    height=0.3\textwidth
    ]
      \addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks, forget plot]
      table {
      0.5 -0.111104291472242
      1.5 -0.0789021132376754
      2.5 -0.0193816275901278
      };
  \end{axis}
\end{tikzpicture}

我可以使用锚点、范围或其他东西来执行此操作吗?

答案1

正如前面提到的,pgfplots知道\addplot graphics这一点。

这里,重要的是:

您必须提供选项xminxmaxymin和 ,ymax以便了解pgfplots图片覆盖的坐标。示例:

\addplot graphics[xmin=0,xmax=3,ymin=-0.12,ymax=0] {example-image.png};

此外,对于axis环境,选项scale only axisenlargelimitsaxis on top很重要。对于第一个,widthheight仅用于缩放轴矩形而不是整个图。如果没有enlargelimits=false图片,则不会覆盖整个轴矩形。并axis on top确保刻度和网格不被图片覆盖。

以下是这些选项的一些展示:

在此处输入图片描述

备注:在第四个例子中,axis on top可以省略而不会改变结果。但如果使用网格,则它们在图片上不可见。

代码:

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
%  \node{\includegraphics[width=0.45\textwidth,height=0.3\textwidth]{example-image.png}};
  \begin{axis}[
    width=0.3\textwidth,
    height=0.2\textwidth,
    scale only axis,
    enlargelimits=false,
    axis on top
  ]
% with dimensions from table
%    \addplot graphics[xmin=0.5,xmax=2.5,ymin=-0.111104291472242,ymax=-0.0193816275901278] {example-image.png};
    \addplot graphics[xmin=0,xmax=3,ymin=-0.12,ymax=0] {example-image.png};
    \addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks, forget plot]
      table {
        0.5 -0.111104291472242
        1.5 -0.0789021132376754
        2.5 -0.0193816275901278
      };
  \end{axis}
\end{tikzpicture}
\hspace{\fill}
\begin{minipage}[b]{0.5\textwidth}
\raggedright
axis:\\
\texttt{scale only axis\\
enlargelimits=false\\
axis on top}\\
graphics:\\
\texttt{xmin=0,xmax=3,\\
ymin=-0.12,ymax=0}\\
\mbox{}
\end{minipage}

\begin{tikzpicture}
%  \node{\includegraphics[width=0.45\textwidth,height=0.3\textwidth]{example-image.png}};
  \begin{axis}[
    width=0.3\textwidth,
    height=0.2\textwidth,
%    scale only axis,
    enlargelimits=false,
    axis on top
  ]
% with dimensions from table
%    \addplot graphics[xmin=0.5,xmax=2.5,ymin=-0.111104291472242,ymax=-0.0193816275901278] {example-image.png};
    \addplot graphics[xmin=0,xmax=3,ymin=-0.12,ymax=0] {example-image.png};
    \addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks, forget plot]
      table {
        0.5 -0.111104291472242
        1.5 -0.0789021132376754
        2.5 -0.0193816275901278
      };
  \end{axis}
\end{tikzpicture}
\hspace{\fill}
\begin{minipage}[b]{0.5\textwidth}
\raggedright
axis:\\
\texttt{enlargelimits=false\\
axis on top}\\
graphics:\\
\texttt{xmin=0,xmax=3,\\
ymin=-0.12,ymax=0}\\
\mbox{}
\end{minipage}

\begin{tikzpicture}
%  \node{\includegraphics[width=0.45\textwidth,height=0.3\textwidth]{example-image.png}};
  \begin{axis}[
    width=0.3\textwidth,
    height=0.2\textwidth,
    scale only axis,
    enlargelimits=false,
%    axis on top
  ]
% with dimensions from table
%    \addplot graphics[xmin=0.5,xmax=2.5,ymin=-0.111104291472242,ymax=-0.0193816275901278] {example-image.png};
    \addplot graphics[xmin=0,xmax=3,ymin=-0.12,ymax=0] {example-image.png};
    \addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks, forget plot]
      table {
        0.5 -0.111104291472242
        1.5 -0.0789021132376754
        2.5 -0.0193816275901278
      };
  \end{axis}
\end{tikzpicture}
\hspace{\fill}
\begin{minipage}[b]{0.5\textwidth}
\raggedright
axis:\\
\texttt{scale only axis\\
enlargelimits=false}\\
graphics:\\
\texttt{xmin=0,xmax=3,\\
ymin=-0.12,ymax=0}\\
\mbox{}
\end{minipage}

\begin{tikzpicture}
%  \node{\includegraphics[width=0.45\textwidth,height=0.3\textwidth]{example-image.png}};
  \begin{axis}[
    width=0.3\textwidth,
    height=0.2\textwidth,
    scale only axis,
%    enlargelimits=false,
    axis on top
  ]
% with dimensions from table
%    \addplot graphics[xmin=0.5,xmax=2.5,ymin=-0.111104291472242,ymax=-0.0193816275901278] {example-image.png};
    \addplot graphics[xmin=0,xmax=3,ymin=-0.12,ymax=0] {example-image.png};
    \addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks, forget plot]
      table {
        0.5 -0.111104291472242
        1.5 -0.0789021132376754
        2.5 -0.0193816275901278
      };
  \end{axis}
\end{tikzpicture}
\hspace{\fill}
\begin{minipage}[b]{0.5\textwidth}
\raggedright
axis:\\
\texttt{scale only axis\\
axis on top}\\
graphics:\\
\texttt{xmin=0,xmax=3,\\
ymin=-0.12,ymax=0}\\
\mbox{}
\end{minipage}

\begin{tikzpicture}
%  \node{\includegraphics[width=0.45\textwidth,height=0.3\textwidth]{example-image.png}};
  \begin{axis}[
    width=0.3\textwidth,
    height=0.2\textwidth,
    scale only axis,
    enlargelimits=false,
    axis on top
  ]
% with dimensions from table
    \addplot graphics[xmin=0.5,xmax=2.5,ymin=-0.111104291472242,ymax=-0.0193816275901278] {example-image.png};
%    \addplot graphics[xmin=0,xmax=3,ymin=-0.12,ymax=0] {example-image.png};
    \addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks, forget plot]
      table {
        0.5 -0.111104291472242
        1.5 -0.0789021132376754
        2.5 -0.0193816275901278
      };
  \end{axis}
\end{tikzpicture}
\hspace{\fill}
\begin{minipage}[b]{0.5\textwidth}
\raggedright
axis:\\
\texttt{scale only axis\\
enlargelimits=false\\
axis on top}\\
graphics:\\
\texttt{xmin=0.5,xmax=2.5,\\
ymin=-0.11110...,ymax=-0.01938...}\\
\mbox{}
\end{minipage}
\end{document}

相关内容