[Tex/LaTex] pgfplots – Number above one of the bars in the bar chart

bar chartlabelspgfplots

I want to put a number above only one of the bars in the bar chart. For example, above the bar A, I want to put a number say 200. Other bars will not have any number at the top. This is my code.

\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.15,
legend style={at={(0.5,-0.2)},
anchor=north,legend columns=-1},
title={Data=30\%},
ylabel={Values},
grid=major,
symbolic x coords={A,B,C,D},
xtick=data,
ytick={6600,6650,6700,6750,6800,6850,6900,6950},
ylabel style={font=\Large},
% nodes near coords,
% nodes near coords align={vertical},
x tick label style={rotate=45,anchor=east},
]
\addplot [MyLineStyle,draw=black,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] coordinates
{
(A,6898.965904)+-(20,20)
(B,6801.9847)+-(20,20)
(C,6734.0923)+-(10,10)
(D,6698.2353)+-(10,10)
};
\end{axis}
\end{tikzpicture}

I want to place a number 200 just above the bar A and leave B, C, D as they are.

Best Answer

In pgfplots you can always access the coordinates of the plot by means of axis cs; in this case, for example, you want to place a label with a number above (axis cs:A,6898.965904) plus the value of the error bar (this is something required otherwise your label will be located inside the error bar).

To place a label it is possible to exploit the usual TikZ syntax:

\node[options] at (location) {label};

thus:

\node[above] at (axis cs:A,6898.965904) {200};

but we should consider also the error bar. I suggest the use of the calc library in such a way:

\node[above] at ($(axis cs:A,6898.965904)+(0,20)$) {200};

This means that the label will be located above a position that comprises the height of the error bar.

A complete MWE:

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.15,
legend style={at={(0.5,-0.2)},
anchor=north,legend columns=-1},
title={Data=30\%},
ylabel={Values},
grid=major,
symbolic x coords={A,B,C,D},
xtick=data,
ytick={6600,6650,6700,6750,6800,6850,6900,6950},
ylabel style={font=\Large},
% nodes near coords,
% nodes near coords align={vertical},
x tick label style={rotate=45,anchor=east},
]
\addplot [draw=black,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] coordinates
{
(A,6898.965904)+-(20,20)
(B,6801.9847)+-(20,20)
(C,6734.0923)+-(10,10)
(D,6698.2353)+-(10,10)
};
\node[above] at ($(axis cs:A,6898.965904)+(0,20)$) {200};
\end{axis}
\end{tikzpicture}

\end{document}

The result:

enter image description here

Related Question