pgfplots:填充使轴环境的 xshift/yshift 选项出现错误

pgfplots:填充使轴环境的 xshift/yshift 选项出现错误

我正在使用pgfplots 库,我想使用和选项来改变环境fillbetween的位置。但是,轴的想象位置和实际绘制的轴的位置似乎存在差异。这仅在至少有一个命令时才会发生。axisxshiftyshifttikzpicturefill between

MWE(灵感来自@marmot相关问题):

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[xshift=1cm,yshift=1cm]
            \addplot[name path=A,domain=0:1] {0};
            \addplot[name path=B,domain=0:1] {x};
            \addplot fill between [of=A and B]; % without this line, problem disappears
        \end{axis}
        \draw[thick,blue] (current axis.south west) rectangle (current axis.north east);
    \end{tikzpicture}
\end{document}

姆韦

如果我使用standalone文档类,或者使用externalTikZ 库,或者在某些文本中插入图形,则图形的右上部分会被裁剪掉:

裁剪

答案1

虽然我不知道是什么原因导致了这种行为,但解决您遇到的问题的一个简单方法是使用密钥at={<coordinate>}

请注意以下示例中的边界框(红色矩形):

在此处输入图片描述

梅威瑟:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}

\begin{document}
    Using \verb|xshift=1cm,yshift=1cm|:

    \begin{tikzpicture}
        \begin{axis}[xshift=1cm,yshift=1cm]
            \addplot[name path=A,domain=0:1] {0};
            \addplot[name path=B,domain=0:1] {x};
            \addplot fill between [of=A and B]; % without this line, problem disappears
        \end{axis}

        \draw[thick,blue] (current axis.south west) rectangle (current axis.north east);
        \draw[thick,red] (current bounding box.south west) rectangle (current bounding box.north east);
    \end{tikzpicture}

    \vspace{1cm}
    Using \verb|at={(1cm,1cm)}|:

    \begin{tikzpicture}
        \begin{axis}[at={(1cm,1cm)}]
            \addplot[name path=A,domain=0:1] {0};
            \addplot[name path=B,domain=0:1] {x};
            \addplot fill between [of=A and B]; % without this line, problem disappears
        \end{axis}

        \draw[thick,blue] (current axis.south west) rectangle (current axis.north east);
        \draw[thick,red] (current bounding box.south west) rectangle (current bounding box.north east);
    \end{tikzpicture}
\end{document}

答案2

这是 PGFPlots 中已知的错误(PGFPlots 追踪器中排名第 199)。您可以通过在选项中添加set layers和来解决此问题。cell picture=trueaxis

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xshift=1cm,
            yshift=1cm,
            % add the following two lines to make it work
            % (for details see comments in bug #199
            %  <https://sourceforge.net/p/pgfplots/bugs/199/>)
            set layers,
            cell picture=true,
        ]
            \addplot[name path=A,domain=0:1] {0};
            \addplot[name path=B,domain=0:1] {x};
            \addplot fill between [of=A and B]; % without this line, problem disappears
        \end{axis}
        \draw[thick,blue] (current axis.south west) rectangle (current axis.north east);
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容