如果它是列表的一部分,为什么我会收到“请求的图层‘背景’不是列表的一部分”错误?

如果它是列表的一部分,为什么我会收到“请求的图层‘背景’不是列表的一部分”错误?

以下代码给出了错误:

! Package pgf Error: Sorry, the requested layer 'background' is not part of the
 layer list. Please verify that you provided \pgfsetlayers and that 'background
' is part of this list.

但是background\pgfsetlayers{background,main}。我做错了什么?

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{fillbetween}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main} 
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis x line=center, 
    axis y line=center, 
    ticks=none
    ]
    \addplot[color=red,domain=-2:2,name path= mycurve]{x^2};
    \addplot[domain=-2:2,name path= myline]{x-1};
\begin{pgfonlayer}{background}
    \addplot[red!10] fill between [of=myline and mycurve, soft clip={domain=-1:1}];
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}
\end{document}

答案1

好的,我明白 Stefan Pinnow 已经展示了问题所在。您需要使用set layersaxis on top(或类似的东西),然后在所需的图层上绘制填充,请参阅 pgfplots 手册第 4.27.2 节。所需的答案似乎是

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis on top,
    axis x line=center, 
    axis y line=center, 
    ticks=none
    ]
    \addplot[color=red,domain=-2:2,name path= mycurve]{x^2};
    \addplot[domain=-2:2,name path= myline]{x-1};
    \addplot[red!10] fill between [of=myline and mycurve, soft clip={domain=-1:1}];
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

在这里,Stefan Pinnow 友情指出,则fill between自动触发阴影区域位于图层上pre main

如果你替换axis on topset layers,你会得到

enter image description here

pgfplots 手册第 4.27.2 节讨论了更多图层选项。如果您想按特定顺序绘制多个重叠区域(和/或轮廓),并且不能仅对各个\addplots 进行排序以提供所需的顺序,则这些附加选项可能很有用。

答案2

当我尝试在轴后面的背景层上绘制某些内容时,我遇到了类似的问题:

\pgfdeclarelayer{background}
\pgfsetlayers{background,main} 
...
\begin{axis}[]
\begin{pgfonlayer}{background}

我收到错误Sorry, the requested layer 'background' is not part of the' is part of this list.

学习部分之后PGFPLOTS 手册 4.27.2,我发现 PGFPLOTS 为轴环境创建了自己的默认层:axis background, axis grid, axis ticks, axis lines, axis tick labels, main, axis descriptions, axis foreground

因此,

\begin{axis}[]
\begin{pgfonlayer}{axis background} % instead of background

对我有用。

相关内容