7

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

2
  • I also know it would be nice to calculate the intersection. I tried to understand the answers here and here, but I couldn´t. Maybe its overkill for a little bit of sine... Apr 28 '14 at 22:07
  • 1
    You need \draw[<->,green!70!black,thick] (axis cs:40,0) -- (axis cs:50,0) node[midway, above,font=\scriptsize] {$\varphi_1$}; Apr 28 '14 at 22:18
6

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

3
  • Thanks again for the fast answer! I always though I could define the node in the middle... proved to be wrong. Apr 28 '14 at 22:28
  • @Paul You're welcome! Indeed, you can place the node in the middle, but after the to part (see second example in my updated answer). Apr 29 '14 at 0:17
  • Ah, so I got that mixed up. Thanks for clarification! Apr 29 '14 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.