[Tex/LaTex] Placing node at path using `midway` in pgfplots

coordinatespgfplotstikz-pgf

I thought my problem would have been possibly answered anywhere here, but I could not get it to work. Questions I consider linked to mine but which weren´t able to solve my issue are node placement problem and pgfplots: placing node on a specific x-position.

I want to make an annotation to a graph drawn using pgfplots (actually, its inside a groupplots-environment). I tried so using the PGF-command draw[<-> (pos a) node [midway,...] -- (pos b). However, the node did not get placed midways, but in the middle of nowhere. This is not true for below and above. However, the same thing happens when I used near end or pos=???.

I worked around this issue just placing a node manually at the place where I want it. However, I don´t find this very satisfactory. Feel free to have a look at the MWE below.


\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}
\begin{tikzpicture}
\begin{axis}[{hide y axis,
axis x line=middle,
height=3cm, width=10cm,
xmin=-22.5, xmax=83,
samples=200,
xtick={-20, 0,20, 40, 60, 80}}]

\addplot+[domain=-20:80,mark=none,green]{sin(40*.18*x)};

% % % % % The erroneous part:
% 1.:
\draw[<->,green!70!black,thick] (axis cs:40,0) 
node[midway, above,font=\scriptsize] {$\varphi_1$} % This SHOULD work, but doesn´t
 -- (axis cs:50,0);

% 2.: 
\node[font=\scriptsize,green!70!black] at (axis cs:45,.3) {$\varphi_1$}; %This does work

\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

Best Answer

You need to place the node either at the end of the path:

\draw[<->,green!70!black,thick] 
  (axis cs:40,0) -- (axis cs:50,0) node[midway, above,font=\scriptsize] {$\varphi_1$};

or after the to part:

\draw[<->,green!70!black,thick] 
  (axis cs:40,0) -- node[midway, above,font=\scriptsize] {$\varphi_1$} (axis cs:50,0);

A complete example:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  hide y axis,
  axis x line=middle,
  height=3cm, width=10cm,
  xmin=-22.5, xmax=83,
  samples=200,
  xtick={-20, 0,20, 40, 60, 80}
]
\addplot+[domain=-20:80,mark=none,green]{sin(40*.18*x)};

\draw[<->,green!70!black,thick] 
  (axis cs:40,0) -- node[midway, above,font=\scriptsize] {$\varphi_1$} (axis cs:50,0);
\draw[<->,red!70!black,thick] 
  (axis cs:10,0) -- (axis cs:20,0) node[midway, above,font=\scriptsize] {$\varphi_2$};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here