[Tex/LaTex] Interaction between “scope + shift” and “fill between” from pgfplots

pgfplotstikz-pgf

I am trying to draw a picture using tikZ and pgfplots. The following is a minimized example of what I am doing.

\documentclass{standalone}
\usepackage{tikz} 
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.12, clip bounding box=default tikz}

\begin{document}
\begin{tikzpicture}
    \draw (0,0) -- (5,5);
    \begin{scope}[shift={(10,0)}] %line A
        \begin{axis}
            \addplot[name path=A, domain=1:3] {2-abs(x-2)};
            \draw[name path=B, thick, black] (axis cs:1,1) -- (axis cs:3,1);
            \addplot[thick, fill=red] fill between[of=A and B] ; %line C
        \end{axis}
    \end{scope} %line B
\end{tikzpicture}
\end{document}

Now, the following happens:

  1. If lines A,B are commented out and line C is not commented out, then the "line" from (0,0) to (5,5) crosses the "filled out" triangle. In particular, the "triangle" is filled out indeed.

  2. If lines A,B are not commented out, but line C is commented out, then the triangle is not filled anymore (as expected) and the "line" from (0,0) to (5,5) is to the left of the actual "plot" containing the triangle (as expected due to the shift).

  3. If none of the lines A,B,C are commented out, then the "line" from (0,0) to (5,5) intersects the triangle (thus, the "shift" from the scope is ignored). Furthermore, the triangle is not filled in red, although the "fill between" is not commented out.

Of course, I would like to have the shift, as well as the filled triangle, but it seems that it is impossible to get both.

I must admit that I do not know much about either TikZ or pgfplots, hence I wonder whether my code is faulty or if I found a bug.

In any circumstance, I would like to know how I can get both the "shift" and the filled triangle. Furthermore, in my actual application, the filled plot is more complex than a triangle, so that I can not simply draw the triangle without pgfplots.

Furthermore, I am drawing two independent "subfigures" and want to place them next to each other, thus the shift.

I am using pdfTeX 3.1415926-2.3-1.40.12 (TeX Live 2011), but I downloaded (and installed) the most recent versions of TikZ and pgfplots today.

Best Answer

Maybe this will help: instead of using a scope, shift the axis in the x direction. Write \begin{axis}[xshift=10cm].

Also, as a sidenote, you can create the triangle using \filldraw[red] (axis cs:1,1) -- (axis cs:2,2) -- (axis cs:3,1) -- cycle;

Full MWE:

\documentclass{standalone}
\usepackage{tikz} 
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \draw (0,0) -- (5,5);
        \begin{axis}[xshift=10cm,xmin=1,xmax=3,ymin=1,ymax=2]
        \filldraw[red] (axis cs:1,1) -- (axis cs:2,2) -- (axis cs:3,1) -- cycle;
        \end{axis}
\end{tikzpicture}
\end{document}

Output

Let me know if you run into any trouble.