[Tex/LaTex] TikZ node connection in pgfplots axis

nodespgfplotstikz-pgf

I have functions plotted in separate axis environments (from pgfplots). In each axis I define a node and I would like to connect these nodes.

In this MWE it doesn't work: The node connection that should be between "sin" and "cos" is way off.

\documentclass{article}

\usepackage{tikz, pgfplots}

\begin{document}

\begin{tikzpicture}

\matrix{
    \begin{axis}
        \addplot {sin(deg(x))};
        \node (sin) at (axis cs:4,1) {sin};
    \end{axis}
    &
    \begin{axis}
        \addplot {cos(deg(x))};
        \node (cos) at (axis cs:-4,1) {cos};
    \end{axis}
    \\
};

\draw (cos) -- (sin);

\end{tikzpicture}

\end{document}

enter image description here

Best Answer

You don't need to put axes in matrix node which does nontrivial things. Use anchors instead

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{positioning}
\pgfplotsset{compat=1.8}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[name = sinax]
        \addplot {sin(deg(x))};
        \node (sin) at (axis cs:4,1) {sin};
    \end{axis}
    \begin{axis}[at={(sinax.outer east)},anchor=outer west]
        \addplot {cos(deg(x))};
        \node (cos) at (axis cs:-4,1) {cos};
    \end{axis}

\draw (cos) -- (sin);

\end{tikzpicture}

\end{document}

enter image description here