[Tex/LaTex] pgfplots: annotate each bar in a group with custom values

annotationsbar chartpgfplots

how can I add custom annotations to each bar in a grouped bar chart?
I know how to display the values from the the bars (with every node near...), but the values I need to display, above or below the bars, are others. And, if possible, rotate them by 90 degrees.

E.g. the first first bar in the first group should get the annotation (13/21)

So far i got this working:

\begin{tikzpicture}
    \begin{axis}[
        width  = \textwidth,
        height = 9cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        grid style={dashed},
        scaled y ticks = false,
        ylabel = {y},
        xlabel = {x},
        symbolic x coords={1,2,3},
        xticklabels={$1$,$2$,$3$},
        xtick = data,
        enlarge x limits=0.3,
        ymin=-3, ymax=3,
        legend style={
            at={(0.5,-0.2)},
            anchor=north,
            column sep=1ex
        },
        legend columns=2,
        legend cell align=left,
    ]

    \addlegendimage{empty legend}
    \addlegendentry{$n = 3$}
    \addlegendimage{empty legend}
    \addlegendentry{}

    \addplot[style={blue,fill=blue,mark=none}]coordinates {(1, 0.714) (2, 1.211) (3, 2.047) };
    \addlegendentry{type 1}
    \addplot[style={gray,fill=gray,mark=none}]coordinates {(1, 1.276) (2, 1.748) (3, 2.249) };
    \addlegendentry{type 2}
    \addplot[style={red,fill=red,mark=none}]coordinates {(1, 2.25) (2, 1.58) (3, 2.499)};
    \addlegendentry{type 3}
    \addplot[style={green,fill=green,mark=none}]coordinates {(1, 1.666) (2, 1.776) (3, 2.499) };
    \addlegendentry{type 4}
    \addplot[style={yellow,fill=yellow,mark=none}]coordinates {(1, 1.2) (2, 1.14) (3, 1.999) };
    \addlegendentry{type 5}
    \addplot[style={black,fill=black,mark=none}]coordinates {(1, 0.2) (2, 0.598) (3, 1.599) };
    \addlegendentry{type 6} 

    \end{axis}
\end{tikzpicture}

Best Answer

As a more principled approach, it turns out that the first solution from How to mark/label nth data point from file in pgfplots? also works for bar plots. The nice thing is that the xshift is taken care of automatically.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\pgfplotsset{
    node near coord/.style args={#1/#2/#3}{% Style for activating the label for a single coordinate
        nodes near coords*={
            \ifnum\coordindex=#1 #2\fi
        },
        scatter/@pre marker code/.append code={
            \ifnum\coordindex=#1 \pgfplotsset{every node near coord/.append style=#3}\fi
        }
    },
    nodes near some coords/.style={ % Style for activating the label for a list of coordinates
        scatter/@pre marker code/.code={},% Reset the default scatter style, so we don't get coloured markers
        scatter/@post marker code/.code={},% 
        node near coord/.list={#1} % Run "node near coord" once for every element in the list
    }
}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = \textwidth,
        height = 9cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        grid style={dashed},
        scaled y ticks = false,
        ylabel = {y},
        xlabel = {x},
        symbolic x coords={1,2,3},
        xticklabels={$1$,$2$,$3$},
        xtick = data,
        enlarge x limits=0.3,
        ymin=-3, ymax=3,
        legend style={
            at={(0.5,-0.2)},
            anchor=north,
            column sep=1ex
        },
        legend columns=2,
        legend cell align=left,
    ]

    \addlegendimage{empty legend}
    \addlegendentry{$n = 3$}
    \addlegendimage{empty legend}
    \addlegendentry{}

    \addplot[style={blue,fill=blue,mark=none},
    nodes near some coords={0/{(13/21)}/above,2/{(17/12)}/above}
    ]coordinates {(1, 0.714) (2, 1.211) (3, 2.047) };
    \addlegendentry{type 1}
    \addplot[style={gray,fill=gray,mark=none}]coordinates {(1, 1.276) (2, 1.748) (3, 2.249) };
    \addlegendentry{type 2}
    \addplot[style={red,fill=red,mark=none}]coordinates {(1, 2.25) (2, 1.58) (3, 2.499)};
    \addlegendentry{type 3}
    \addplot[style={green,fill=green,mark=none}]coordinates {(1, 1.666) (2, 1.776) (3, 2.499) };
    \addlegendentry{type 4}
    \addplot[style={yellow,fill=yellow,mark=none}]coordinates {(1, 1.2) (2, 1.14) (3, 1.999) };
    \addlegendentry{type 5}
    \addplot[style={black,fill=black,mark=none}]coordinates {(1, 0.2) (2, 0.598) (3, 1.599) };
    \addlegendentry{type 6} 
    \end{axis}
\end{tikzpicture}
\end{document}

Output