[Tex/LaTex] pgfplots – Add offset to pins

nodespgfplotspositioningtikz-pgf

I'm trying to make a bar plot for an optimization parameter in quantum physics. To be more informative, the graph should display the values of these parameters above the bars. So far I did

\documentclass{minimal}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[title={$E_0$ über $n$}, ymax=2.5, yticklabels=]
      \addplot [blue!90!white, fill=blue!50!white, ybar] coordinates {
        ( 1, 1.96556 )
        ( 2, 1.86105 )
        ( 3, 1.93185 )
        ( 4, 2.02568 )
        ( 5, 2.12077 )
        ( 6, 2.21219 )
      };
      \pgfplotsforeachungrouped \x/\y in {1/1.96556, 2/1.86105, 3/1.93185, 4/2.02568, 5/2.12077, 6/2.21219} {
        \edef\temp{\noexpand\node[coordinate, pin={[rotate=90]0:\y}] at (axis cs:\x,\y) {};}\temp
      }
    \end{axis}
  \end{tikzpicture}
\end{center}

\end{document}

This code outputs the following graph

Optimization for a particle in the gravitational field

Now I tried to add a little space between the bars and the pins, but I failed. I tried ++ (axis cs:0,0.1)

\edef\temp{\noexpand\node[coordinate, pin={[rotate=90]0:\y}] at (axis cs:\x,\y) ++ (axis cs:0,0.1) {};}\temp

and ++ (axis direction cs:0,0.1)

\edef\temp{\noexpand\node[coordinate, pin={[rotate=90]0:\y}] at (axis cs:\x,\y) ++ (axis direction cs:0,0.1) {};}\temp

Both variants give this error:

! Package tikz Error: A node must have a (possibly empty) label text.

I also tried pos=0.1

\edef\temp{\noexpand\node[coordinate, pin={[rotate=90]0:\y},pos=0.1] at (axis cs:\x,\y) {};}\temp

which compiles fine but gives this output

Broken version of the upper graph

Thanks in advance, maeru.

Best Answer

I would stick to the pgfplots as much as I can for the node style. You can pass pin options to the nodes near coords and also many other options (colors, linestyles etc.) as follows:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[title={$E_0$ über $n$}, ymax=2.5, yticklabels=,
nodes near coords={\pgfmathprintnumber[precision=4]\pgfplotspointmeta},
nodes near coords align={
     shift={(0,1.5cm)},
     rotate=90,
     pin={[pin distance = 0.75cm,
          pin edge={thick,double=yellow}]180:},
     text=red
}
]
  \addplot [blue!90!white, fill=blue!50!white, ybar] coordinates {
    ( 1, 1.96556 )
    ( 2, 1.86105 )
    ( 3, 1.93185 )
    ( 4, 2.02568 )
    ( 5, 2.12077 )
    ( 6, 2.21219 )
  };

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Related Question