![在顶层绘制 x 刻度标签](https://linux22.com/image/383371/%E5%9C%A8%E9%A1%B6%E5%B1%82%E7%BB%98%E5%88%B6%20x%20%E5%88%BB%E5%BA%A6%E6%A0%87%E7%AD%BE.png)
对于一组图,我想要将所有 x 刻度标签放在顶层,以便在 tikzpicture 环境末尾绘制的线条位于标签下方的图层上。
我已经尝试设置图层,on top layer
但不幸的是scope
似乎总是需要在特定图层上绘图的环境。
下面我的例子说明了这个问题。目标是最终将 x=2 处的 x 刻度标签节点放置在虚线的顶部。
例子
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz-layers}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{backgrounds}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture]
\begin{groupplot}[
no markers,
xmax=6,
ymin=0,
every x tick label/.style={
fill=red
},
group style = {
group size = 1 by 2,
},
]
\nextgroupplot[ymax=15]
\addplot +[domain=0:6] {0.5*x^2};
\coordinate (t1g1-top) at (axis cs:2,15);
\coordinate (t1g1-bottom) at (axis cs:2,0);
\nextgroupplot
\addplot +[domain=0:6] {x};
\coordinate (t1g2-bottom) at (axis cs:2,0);
\end{groupplot}
\begin{scope}[on above layer]
\draw[dashed, ultra thick] (t1g1-top) -- (t1g2-bottom);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
最简单的方法,只有很小的副作用,就是将选项set layers
与选项结合使用,以axis on top
实现您想要的效果。两个选项都应添加到axis
选项中。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
% ---------------------------------------------------------------------
% activate layers
set layers,
% and draw axis lines and labels on "top layers"
axis on top,
% ---------------------------------------------------------------------
no markers,
xmax=6,
ymin=0,
every x tick label/.style={
fill=red
},
group style = {
group size = 1 by 2,
},
]
\nextgroupplot[ymax=15]
\addplot+ [domain=0:6] {0.5*x^2};
\coordinate (t1g1-top) at (axis cs:2,15);
\nextgroupplot
\addplot+ [domain=0:6] {x};
\coordinate (t1g2-bottom) at (axis cs:2,0);
\end{groupplot}
\draw[dashed, ultra thick] (t1g1-top) -- (t1g2-bottom);
\end{tikzpicture}
\end{document}