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'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.

share|improve this question
    
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. While solving problems is fun, setting them up is not. Then those trying to help can simply cut and paste your MWE and get started on solving the problem. –  Peter Grill Feb 4 '13 at 21:38
    
I added a MWE now. Sorry for inconvenience. –  Henri Menke Feb 4 '13 at 21:42
    
@maeru: Instead of editing the question to include the solution you went with, you should post an answer yourself. That keeps the site more organised. –  Jake Feb 4 '13 at 22:11

3 Answers 3

up vote 12 down vote accepted

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

share|improve this answer
    
+1: Excellent answer! I couldn't figure out how to make the pin line go straight up with the rotated text. Doing it the other way round (with an empty pin) is a great idea –  Jake Feb 4 '13 at 21:54
    
Can I adjust the precision of the labels and/or control their color? –  Henri Menke Feb 4 '13 at 22:03
    
@maeru Ah I forgot that completely. Sorry. I'll edit this in a minute. –  percusse Feb 4 '13 at 22:04
    
Perfect answer! Thank you very much. –  Henri Menke Feb 4 '13 at 22:17

One way to do hat is to use tikz's calc library to adjust the coordinate of where the pin is being placed:

($(axis cs:\x,\y)+(0.0cm,0.1cm)$)

enter image description here

Code:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\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)+(0.0cm,0.1cm)$) {};}\temp
      }
    \end{axis}
  \end{tikzpicture}
\end{center}

\end{document}
share|improve this answer

My solution

There are great answers to this, but I took the path of least resistance and just added shift={(0,0.1cm)}

\edef\temp{\noexpand\node[coordinate,shift={(0,0.1cm)},pin = {[rotate=90]0:\y}] at (axis cs:\x,\y) { };}
share|improve this answer

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.