6

I have already seen Filling path in pgfplot and pushing into background? - that doesn't help me much because it's simply about axis on top. I've also seen Why does pgfplots package cause tikz layer to disappear? - I don't think I'm hitting that bug, as I'm on pgfplots 1.5.1 (and the example in the answer there works for me as is).

Anyways, this is my MWE - in it, I'd like the strokes of the cos(x) function to be drawn behind the strokes of the sin(x) - even if cos(x) is plotted after sin(x) (this is a stand-in for my eventual use case, where I'd like to calculate some intersections based on a plot, and draw them behind the plot):

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\pgfdeclarelayer{front}
\pgfdeclarelayer{back}
\pgfsetlayers{back,main,front}
% \pgfsetlayers{back,front}

\begin{tikzpicture}
  \begin{axis}
    \begin{pgfonlayer}{front}
      \filldraw (0.5,0.5)--(1.5,0.5)--(1.5,1.5)--(0.5,1.5)--cycle;
      \addplot[name=one,blue,mark=+,domain=1:64] {sin(deg(x))};
    \end{pgfonlayer}
    \begin{pgfonlayer}{back}
      \addplot[name=two,red,mark=+,domain=1:64] {cos(deg(x))};
      \filldraw [red] (0,0) --(2,0)--(2,2)--(0,2)--cycle;
    \end{pgfonlayer}
    \end{axis}
    \filldraw [green] (0,0) --(1,0)--(1,1)--(0,1)--cycle;
\end{tikzpicture}

\end{document}

It produces something like this:

test_pdf

... zoomed in evince:

zoom_evince

The problems here are:

  • even if the cos is supposedly drawn in the "back" layer - its strokes (red) are still above the sin (blue)
  • If I use \pgfsetlayers{back,front}, instead of {back,main,front} - I get a blank output; which probably means that the plots are drawn on main anyway (with layering controlled by order of addplot commands), and so drawing in back or front layer fails
  • The \addplots are shown - however, the \filldraws within the axis environment aren't

Is it possible to use layering in this sense in a pgf plot, within an axis environment?

  • 1
    I get it right. Can you magnify you pdf and see please? – user11232 Feb 17 '14 at 0:05
  • 3
    current version is 1.9. so updating will fix many things and will add improvements regardless of your issue. – percusse Feb 17 '14 at 7:33
  • Thanks @HarishKumar - added a zoomed view (click on the images, they're linked to full size). – sdaau Feb 19 '14 at 22:46
  • 1
    Is there a specific reason you can't update the package? Your MWE works as you expect for me on v1.9, as others have noted. This is likely to be closed as off-topic since that is the convention for issues resolved by package updates here. – Paul Gessler Mar 2 '14 at 16:18
  • 5
    This question appears to be off-topic because it is about an outdated version of pgfplots. – Marco Daniel Mar 2 '14 at 17:32

Browse other questions tagged or ask your own question.