12

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.

  • 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
13

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

  • +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
6

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}
1

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) { };}

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.