如何使标签(节点)出现在 pgfplots 中的标记前面?
例如:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {
(1, 1)
} node[pos=0.5]{\color{green}{LABEL}};
\end{axis}
\end{tikzpicture}
\end{document}
请注意,“LABEL”出现在情节标记后面:
它可能是一个很好的默认值(或从 TikZ 继承)但我该如何改变这种行为?(最好是仍然允许使用 -syntax 的东西...} node[]{...};
。)
答案1
axis
通常,标记会绘制在环境中所有图的顶部(clip mode=global
默认),或者至少绘制在单个图的顶部(clip mode=individual
),以避免剪切标记。在您的代码中,节点是路径的一部分plot
,因此位于标记后面。
版本 1
axis tick labels
您可以移动位于图层后面的图层中的标记main
:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
set layers,% using layers
mark layer=axis tick labels% defines the layer of the marks
}
\begin{axis}
\addplot coordinates {
(0.5,0.5) (1, 1) (1.5,1.5)
} node[pos=0.5]{\color{green}{LABEL}};
\end{axis}
\end{tikzpicture}
\end{document}
请注意,所有标记都将位于同一轴环境中的所有图的后面。
版本 2
在绘图路径中设置,并在图层上的环境coordinate
中绘制节点:pgfonlayer
axis foreground
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{set layers}% using layers
\begin{axis}[set layers]
\addplot coordinates {
(0.5,0.5) (1, 1) (1.5,1.5)
} coordinate[pos=0.5](p1);% coordinate in the middle of the plot path
\begin{pgfonlayer}{axis foreground}
\node at (p1){\color{green}{LABEL}};% draw the node in the axis foreground
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}
\end{document}
不幸的是,我不知道如何在选项中直接更改节点的层。但也许有这种可能性。
版本 3
使用clip mode=invidual
,在绘图路径中设置coordinate
,并在绘图完成后绘制节点:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[clip mode=individual]
\addplot coordinates {
(0.5,0.5) (1, 1) (1.5,1.5)
} coordinate[pos=0.5](p1);% coordinate in the middle of the plot path
\node at (p1){\color{green}{LABEL}};
\end{axis}
\end{tikzpicture}
\end{document}
编辑
摘自手册 v. 1.10,第 349 页:
层
layers/standard
配置定义为\pgfplotsset{ layers/standard/.define layer set= {axis background,axis grid,axis ticks,axis lines,axis tick labels,main,% axis descriptions,axis foreground} { grid style= {/pgfplots/on layer=axis grid}, tick style= {/pgfplots/on layer=axis ticks}, axis line style= {/pgfplots/on layer=axis lines}, label style= {/pgfplots/on layer=axis descriptions}, legend style= {/pgfplots/on layer=axis descriptions}, title style= {/pgfplots/on layer=axis descriptions}, colorbar style= {/pgfplots/on layer=axis descriptions}, ticklabel style= {/pgfplots/on layer=axis tick labels}, axis background@ style={/pgfplots/on layer=axis background}, 3d box foreground style={/pgfplots/on layer=axis foreground}, }, }
您可以定义一个新的图层集,例如在后面添加一个新图层main
:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{
layers/mylayers/.define layer set=
{axis background,axis grid,axis ticks,axis lines,axis tick labels,%
axis mymarks,% additional layer behind main
main,axis descriptions,axis foreground}
{/pgfplots/layers/standard}% re-uses the style definitions of layers/standard
}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
set layers=mylayers,% using mylayers
mark layer=axis mymarks% defines the layer of the marks
}
\begin{axis}
\addplot coordinates {
(0.5,0.5) (1, 1) (1.5,1.5)
} node[pos=0.5]{\color{green}{LABEL}};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
类似这样。此解决方案需要background, main and foreground
技巧
代码
\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\pgfplotsset{compat=1.10}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\begin{document}
\begin{tikzpicture}
\begin{axis}
%\begin{scope}{background}
\addplot coordinates {
(1, 1)
};
%\end{scope}
\begin{pgfonlayer}{foreground}
\node at (axis cs: 1,1) [pos=0.5]{\color{red}LABEL};
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}
\end{document}