Labels above error bars

bartikz-pgf

Can someone help me to place these labels above the error bars? And is there any option to show the SDs besides the label?

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.18}
\begin{document}

\usetikzlibrary{patterns}
\begin{tikzpicture}

\begin{axis}[
    ybar, axis on top,
    height=11cm, width=14.5cm,
    bar width=0.4cm,
    ybar=3pt,
    enlarge y limits={value=.1,upper},
    ymin=25, ymax=400,
    axis x line*=bottom,
    axis y line*=left,
    y axis line style={opacity=0},
    tickwidth=1pt,
    enlarge x limits=true,
     ymajorgrids=true,
major grid style={lines,lightgray},
    legend style={
        at={(0.5,-0.1)},
        anchor=north,
        legend columns=5,
        /tikz/every even column/.append style={column sep=0.5cm}},
    ylabel=\textbf{Weekly accumulated load (\% of match)},
    symbolic x coords={TD (m),HSR (m),SPR (m),Acc. (\#),Dec. (\#)},
xtick=data, 
ticklabel style={/pgf/number format/.cd, use comma, 1000 sep = {}},
    nodes near coords,
nodes near coords align={vertical},
nodes near coords={\pgfkeys{/pgf/fpu}\pgfmathparse{\pgfplotspointmeta}\pgfmathprintnumber{\pgfmathresult}\,\%},
 every node near coord/.append style={font=\scriptsize, rotate=0, yshift=20},
every axis legend/.append style={font=\scriptsize}],
    \addplot[draw=none, postaction={pattern=north east lines}, fill=white, error bars/.cd,y dir=both,y explicit,error bar style={line width=0.1pt}]
        coordinates 
        {
        (TD (m), 225) +- (TD (m),24)
        (HSR (m),142) +- (HSR (m),31)
        (SPR (m), 235) +- (SPR (m),54)
        (Acc. (\#), 279) +- (Acc. (\#),38)
        (Dec. (\#), 225) +- (Dec. (\#),56)
        };
    \addplot[draw=none, postaction={pattern=dots},fill=gray, pattern color= black,error bars/.cd,y dir=both,y explicit,error bar style={line width=0.1pt}]
         coordinates {
         (TD (m), 190) +- (TD (m),18)
         (HSR (m),88) +- (HSR (m),20) 
         (SPR (m), 112) +- (SPR (m),28) 
         (Acc. (\#), 247) +- (Acc. (\#),40) 
         (Dec. (\#), 188) +- (Dec. (\#),33)
         };
    \addplot[draw=none, fill=black, error bars/.cd,y dir=both,y explicit,error bar style={line width=0.1pt}]
         coordinates {
         (TD (m), 190) +- (TD (m),11) 
         (HSR (m),123) +- (HSR (m),21) 
         (SPR (m), 169) +- (SPR (m),39)  
         (Acc. (\#), 212) +- (Acc. (\#),29) 
         (Dec. (\#), 158) +- (Dec. (\#),30)
         };
    \addplot[draw=none, fill=lightgray, error bars/.cd,y dir=both,y explicit, error bar style={line width=0.1pt}]
         coordinates {
         (TD (m), 199) +- (TD (m),17) 
         (HSR (m),103) +- (HSR (m),21) 
         (SPR (m), 116) +- (SPR (m),25) 
         (Acc. (\#), 253) +- (Acc. (\#),30) 
         (Dec. (\#), 169) +- (Dec. (\#),18)
         };
        \legend{CB, WB, CM, CF}
\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

Best Answer

This is a solution based on https://tex.stackexchange.com/a/130318/263192 and https://tex.stackexchange.com/a/200575/263192.

I have moved your data into pgfplotstable tables, such that the data can be referenced in visualization depends on.

Unfortunately, this only works up to compat=1.10.

\documentclass[border=10]{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.10}
\usepackage{pgfplotstable} 

\pgfplotstableread[col sep=comma]{
X, Y, E
TD (m), 225, 24
HSR (m),142, 31
SPR (m), 235, 54
Acc. (\#), 279, 38
Dec. (\#), 225, 56
}\tA

\pgfplotstableread[col sep=comma]{
X, Y, E
TD (m), 190, 18
HSR (m),88, 20
SPR (m), 112, 28
Acc. (\#), 247, 40
Dec. (\#), 188, 33
}\tB

\pgfplotstableread[col sep=comma]{
X, Y, E
TD (m), 190, 11
HSR (m),123, 21
SPR (m), 169, 39
Acc. (\#), 212, 29
Dec. (\#), 158, 30
}\tC

\pgfplotstableread[col sep=comma]{
X, Y, E
TD (m), 199, 17
HSR (m),103, 21
SPR (m), 116, 25
Acc. (\#), 253, 30
Dec. (\#), 169, 18
}\tD

\begin{document}
\usetikzlibrary{patterns}
\begin{tikzpicture}

\begin{axis}[
    ybar, axis on top,
    height=11cm, width=19cm,
    bar width=0.6cm,
    ybar=3pt,
    enlarge y limits={value=.1,upper},
    ymin=25, ymax=400,
    axis x line*=bottom,
    axis y line*=left,
    y axis line style={opacity=0},
    tickwidth=1pt,
    enlarge x limits=true,
    ymajorgrids=true,
    major grid style={lightgray},
    legend style={
        at={(0.5,-0.1)},
        anchor=north,
        legend columns=5,
        /tikz/every even column/.append style={column sep=0.5cm}},
    ylabel=\textbf{Weekly accumulated load (\% of match)},
    symbolic x coords={TD (m),HSR (m),SPR (m),Acc. (\#),Dec. (\#)},
    xtick=data, 
    ticklabel style={/pgf/number format/.cd, use comma, 1000 sep = {}},
    nodes near coords,
    nodes near coords align={vertical},
    nodes near coords={\pgfkeys{/pgf/fpu}\pgfmathparse{\pgfplotspointmeta}\pgfmathprintnumber{\pgfmathresult}\,\%},
    visualization depends on=\thisrow{E} \as \sd,
    every node near coord/.append style={
        font=\scriptsize,
        rotate=0,
        shift={(0,transformdirectiony(\sd))}
    },
    every axis legend/.append style={font=\scriptsize}
]
    \addplot[draw=none, postaction={pattern=north east lines}, fill=white, error bars/.cd,y dir=both,y explicit,error bar style={line width=0.1pt}]
        table[x=X,y=Y,y error=E] {\tA};
    \addplot[draw=none, postaction={pattern=dots},fill=gray, pattern color= black,error bars/.cd,y dir=both,y explicit,error bar style={line width=0.1pt}]
        table[x=X,y=Y,y error=E] {\tB};
    \addplot[draw=none, fill=black, error bars/.cd,y dir=both,y explicit,error bar style={line width=0.1pt}]
        table[x=X,y=Y,y error=E] {\tC};
    \addplot[draw=none, fill=lightgray, error bars/.cd,y dir=both,y explicit, error bar style={line width=0.1pt}]
        table[x=X,y=Y,y error=E] {\tD};
    \legend{CB, WB, CM, CF}
\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

Related Question