# Interaction between "scope + shift" and "fill between" from pgfplots

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}
\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.

• If subfigures are independent, there's no need to include both in same tikzpicture you'll find a lot of questions and corresponding answers explaining how to align figures. May 15, 2015 at 16:13
• This is the way to move an axis environment. But it doesn't work when addplot ... fill between ... is used. Although if you insist with two subfigures inside one tikzpicture. you can do it the other way round: draw the axis at origin (as pgfplots insists) and use a shifted scope for the other one. May 15, 2015 at 16:33

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}


Let me know if you run into any trouble.

• Thank you very much, the "xshift" option exactly solved my problem. Only drawing the triangle "by hand" without the "fill between" option does not work in my case, since the actual plot is more complex, but everything now works as it should! May 17, 2015 at 15:25