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:
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.
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).
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.
tikzpicture
you'll find a lot of questions and corresponding answers explaining how to align figures.axis
environment. But it doesn't work whenaddplot ... fill between ...
is used. Although if you insist with two subfigures inside onetikzpicture
. you can do it the other way round: draw the axis at origin (aspgfplots
insists) and use a shiftedscope
for the other one.