1

I'm trying to draw an extremely customized background grid, one that does not consist of lines extending all the way from one side of the plot to the other, and I want this grid to be drawn underneath the actual data plot. I believe that the appropriate way to do this is to draw the grid as "custom annotations" (as the pgfplots manual uses that term) on a specific layer of the plot. Complicating matters slightly, the data plot uses "fill between", which I understand to be somewhat troublesome.

I have been able to figure out how to move graphics drawn by \addplot to the "axis grid" layer, but not graphics drawn by TikZ primitives. I need to use TikZ primitives because I need to use calc to make the lines extend outside the plot area by a specified absolute distance (to make them continuous with "outside" ticks; see this question for why I can't just let pgfplots draw the ticks).

Concretely, this gets drawn underneath a "fill between":

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

But in this, the "on layer" bit has no effect and the line is drawn on top of the "fill between".

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

What can I do to make \draw commands and the like be placed on a specific layer?


MWE follows. Rendered:

enter image description here

Code:

\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}
5
  • Before trying to understand any of this, I want to know if you know the following: With clip=false \addplot most definitely can extend outside the axis area. You do not need calc to extend a line you can use shorten > and shorten < -see my previous answer. If you prefer, you can also use xshift and yshift on the coordinate - maybe even better. on layer can as far as know/remember not be used as option to \addplot. Commented Mar 1 at 16:27
  • Most important: things are drawn in the order they appear. Without understanding your use case or code, I do not see anything that needs layers!! Commented Mar 1 at 16:29
  • 1
    Why do you feel the need to attack me? I just asked clarifying questions and I just helped you with your previous question. Commented Mar 1 at 18:37
  • I don't understand. I only stated that I need an answer to the question I asked and not a workaround. No hostility was intended.
    – zwol
    Commented Mar 1 at 19:32
  • @JeT: If you feel for answering , then just go ahead. I often see something that is rude or I myself write something that can be read that way. Commented Mar 2 at 9:20

1 Answer 1

1

enter image description here

\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}
2
  • @zwol is it what you were looking for ?
    – JeT
    Commented Mar 9 at 12:52
  • 1
    \begin{pgfonlayer} is indeed what I was looking for, thank you.
    – zwol
    Commented Mar 15 at 22:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .