Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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... –  Paul Paulsen Apr 28 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$}; –  Gonzalo Medina Apr 28 at 22:18

1 Answer 1

up vote 7 down vote accepted

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

share|improve this answer
    
Thanks again for the fast answer! I always though I could define the node in the middle... proved to be wrong. –  Paul Paulsen Apr 28 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). –  Gonzalo Medina Apr 29 at 0:17
    
Ah, so I got that mixed up. Thanks for clarification! –  Paul Paulsen Apr 29 at 6:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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