特定 pgfplots 层上的自定义绘图命令

特定 pgfplots 层上的自定义绘图命令

我正在尝试绘制一个高度自定义的背景网格,该网格不包含从图的一侧延伸到另一侧的线条,并且我希望将此网格绘制在实际数据图下方。我认为执行此操作的适当方法是将网格绘制为“自定义注释”(正如 pgfplots 手册使用该术语)在地块的特定层上稍微复杂化一点的是,数据图使用了“填充”,据我所知这有点麻烦。

我已经能够弄清楚如何将由 绘制的图形移动\addplot到“轴网格”层,但不能将由 TikZ 基元绘制的图形移动到“轴网格”层。我需要使用 TikZ 基元,因为我需要使用calc使线条在绘图区域外延伸指定的距离绝对距离(使它们与“外部”刻度连续;参见这个问题为什么我不能只让 pgfplots 绘制刻度)。

具体来说,这是在“填充之间”绘制的:

\addplot[on layer=axis grid, draw=blue, no markers]
    coordinates {
        (10, \pgfkeysvalueof{/pgfplots/ymin})
        (10, 0)
    }
;

但在此,“在层上”位没有效果,并且线条绘制在“填充之间”的顶部。

\begin{scope}[style={draw=red, /pgfplots/on layer=axis grid}]
\draw ($(20, \pgfkeysvalueof{/pgfplots/ymin}) - (0mm, 1mm)$) --
      (20, 0) ;
\end{scope}

我该怎么做才能将\draw命令等放置在特定的图层上?


MWE 如下。渲染结果:

在此处输入图片描述

代码:

\documentclass[10pt,tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{fillbetween}
% The manual says compat=1.15 is the highest level that actually does anything.
\pgfplotsset{compat=1.15, set layers}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xmin=0, xmax=30,
  ymin=-10, ymax=10,
  xtick={0, 10, 20, 30},
  ytick={-10, 0, 10},
  axis x line=bottom,
  axis y line=left,
  axis line style={draw=none},
  tick style={draw=none},
  clip=false
]
% This line is drawn underneath the "fill between" plot, as desired,
% but cannot be made to extend 1mm outside the plot area AFAICT.
\addplot[on layer=axis grid, draw=blue, no markers]
    coordinates {
        (10, \pgfkeysvalueof{/pgfplots/ymin})
        (10, 0)
    }
;
% This line _can_ be made to extend 1mm outside the plot area, but
% despite the "on layer" key in the style, is drawn on top of the
% "fill between" plot.
\begin{scope}[style={draw=red, /pgfplots/on layer=axis grid}]
\draw ($(20, \pgfkeysvalueof{/pgfplots/ymin}) - (0mm, 1mm)$) --
      (20, 0) ;
\end{scope}
% I also tried this. The manual makes it sound like it should work,
% but it just produces a warning message ("the current plot has no
% coordinates") and draws nothing.
\addplot[on layer=axis grid, draw=magenta, no markers]
    coordinates {}
    ($(15, \pgfkeysvalueof{/pgfplots/ymin}) - (0mm, 1mm)$) --
      (15, 0) ;
\addplot[draw=none, no markers, name path=lower]
    coordinates { (0, -10) (30, 0) }
;
\addplot[draw=none, no markers, name path=upper]
    coordinates { (0, 10) (30, 0) }
;
\addplot[black] fill between [of=lower and upper] ;
\draw [draw=gray,very thin]
   (\pgfkeysvalueof{/pgfplots/xmin}, \pgfkeysvalueof{/pgfplots/ymin}) --
   (\pgfkeysvalueof{/pgfplots/xmax}, \pgfkeysvalueof{/pgfplots/ymin}) ;
\end{axis}
\end{tikzpicture}
\end{document}

答案1

在此处输入图片描述

\documentclass[10pt,tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.15, set layers}

\def\xmin{\pgfkeysvalueof{/pgfplots/xmin}}
\def\xmax{\pgfkeysvalueof{/pgfplots/xmax}}
\def\ymin{\pgfkeysvalueof{/pgfplots/ymin}}
\def\ymax{\pgfkeysvalueof{/pgfplots/ymax}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xmin=0, xmax=30,
  ymin=-10, ymax=10,
  xtick={0, 10, 20, 30},
  ytick={-10, 0, 10},
  axis x line=bottom,
  axis y line=left,
  axis line style={draw=none},
  tick style={draw=none},
  clip=false
]
\addplot[on layer=axis grid, draw=blue, no markers]
    coordinates {
        (10, \ymin)
        (10, 0)
    }
;
\begin{pgfonlayer}{axis grid}
\draw[style={draw=red}] ($(20, \ymin) - (0mm, 1mm)$) --
      (20, 0) ;
\end{pgfonlayer}
\addplot[draw=none, no markers, name path=lower]
    coordinates { (0, -10) (30, 0) }
;
\addplot[draw=none, no markers, name path=upper]
    coordinates { (0, 10) (30, 0) }
;
\addplot[black] fill between [of=lower and upper] ;
\draw [draw=gray,very thin]
   (\xmin, \ymin) --
   (\xmax, \ymin) ;
\end{axis}
\end{tikzpicture}
\end{document}

相关内容