# Pgfplots: shift single nodes with nodes near coords

I use the following code. Is there a way to shift specific nodes of "nodes near coords" without placing every node itself which is a lot of work? The nodes at "Fichte" and "Kiefer" are overlapping with the bar. Thanks for the help!

\documentclass[12pt,a4paper]{scrreprt}
\usepackage{filecontents}
\usepackage{pgfplots}

\begin{document}

\begin{filecontents*}{gruen.dat}
Eiche;1.1;0.975
Buche;1.68;1.45
ALH;0.77;0.63
ALN;1.14;1.05
Fichte;2.76;2.98
Tanne;0.18;0.16
Douglasie;0.22;0.18
Kiefer;2.43;2.6
Lärche;0.3;0.29
\end{filecontents*}

\begin{figure} [ht]
\begin{tikzpicture}
\begin{axis}[
ylabel={Mio. ha},
width=\textwidth, height=7cm,
ybar,
bar width=5pt,
symbolic x coords=
{Eiche,Buche,ALH,ALN,Fichte,Tanne,Douglasie,Kiefer,Lärche},
xtick=data,
table/header=false,% <- keine Kopfzeile in der Datentabelle
legend style={at={(0.2,0.94)},
anchor=north},
xticklabel style={rotate=35},
]

\addplot [fill=lightgray!30] table [y index=2, col sep=semicolon]  {gruen.dat};
\addplot [fill=lightgray!110, nodes near coords] table [y index=1, col     sep=semicolon] {gruen.dat};
\legend{BWI 2002, BWI 2012}
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}


I guess you do not really want to shift individual shifts, but just make sure that the nodes are always above the highest bar. This can be achieved with

\documentclass[12pt,a4paper]{scrreprt}
\usepackage{filecontents}
\usepackage{pgfplots}
\begin{document}

\begin{filecontents*}{gruen.dat}
Eiche;1.1;0.975
Buche;1.68;1.45
ALH;0.77;0.63
ALN;1.14;1.05
Fichte;2.76;2.98
Tanne;0.18;0.16
Douglasie;0.22;0.18
Kiefer;2.43;2.6
Lärche;0.3;0.29
\end{filecontents*}

\begin{figure} [ht]
\begin{tikzpicture}
\begin{axis}[
ylabel={Mio. ha},
width=\textwidth, height=7cm,
ybar,
bar width=5pt,
symbolic x coords=
{Eiche,Buche,ALH,ALN,Fichte,Tanne,Douglasie,Kiefer,Lärche},
xtick=data,
table/header=false,% <- keine Kopfzeile in der Datentabelle
legend style={at={(0.2,0.94)},
anchor=north},
xticklabel style={rotate=35},
]

\addplot [fill=lightgray!30] table [y index=2, col sep=semicolon]  {gruen.dat};
\addplot [fill=lightgray!110] table [y index=1, col     sep=semicolon] {gruen.dat};
\addplot [visualization depends on={\thisrowno{1} \as \myvalue},
only marks,
nodes near coords,
nodes near coords={\pgfmathprintnumber{\myvalue}},
nodes near coords style={xshift=\pgfkeysvalueof{/pgf/bar width},above}] table
[x index=0,y expr={max(\thisrowno{1},\thisrowno{2})}, col sep=semicolon] {gruen.dat};
\legend{BWI 2002, BWI 2012}
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}


UPDATE: was confused about the rows, thanks to Stefan Pinnow!

• Now you plot the values of the "higher" instead of the "dark gray" bars, i.e. 2.98 instead of 2.76 ... – Stefan Pinnow Mar 5 '18 at 16:06
• (Also you missed to remove the xshift stuff in the nodes near coords style from Zarko's answer or was that intended to be still present?) – Stefan Pinnow Mar 5 '18 at 16:12
• @StefanPinnow Thanks for your first comment, and as for the second: did you realize that the OP wants to have the value above the dark bar? – user121799 Mar 5 '18 at 16:31
• True, you need it because you add it as third \addplot, but then it still is wrong. To visualize that change bar width e.g. to 15pt. Correct would be xshift={(\pgfplotbarwidth + 2pt)/2} where the 2pt come from the default value of bar shift auto which cannot be accessed by \pgfkeysvalueof (even if you add it to the axis options -- for whatever reason). – Stefan Pinnow Mar 5 '18 at 17:23
• Thank you for the answer! I prefer this one because the figure will be used in a presentation where I got much less horizontal space, so increasing the bar width is no option. I also prefer the answer because it is more flexible. – Felix Phl Mar 5 '18 at 18:54

you have problem where bare with nodes is lower then bar without it. two solutions:

• make bars wider (that above them is enough place for nodes content)
• redefine positions of nodes above bars

combining bot solutions gives:

\begin{filecontents*}{gruen.dat}
Eiche;1.1;0.975
Buche;1.68;1.45
ALH;0.77;0.63
ALN;1.14;1.05
Fichte;2.76;2.98
Tanne;0.18;0.16
Douglasie;0.22;0.18
Kiefer;2.43;2.6
Lärche;0.3;0.29
\end{filecontents*}

\documentclass{beamer}
\usepackage{filecontents}
\usepackage{pgfplots}

\begin{document}
\begin{frame}[fragile]
\begin{tikzpicture}
\begin{axis}[
ylabel={Mio. ha},
width=\textwidth, height=7cm,
ybar,
bar width=9pt,
symbolic x coords=
{Eiche,Buche,ALH,ALN,Fichte,Tanne,Douglasie,Kiefer,Lärche},
xtick=data,
table/header=false,% <- keine Kopfzeile in der Datentabelle
legend style={at={(0.2,0.94)},
anchor=north},
xticklabel style={rotate=35},
% for positioning nodes over bars
nodes near coords style={anchor=south west, inner xsep=0pt,
xshift=-0.5\pgfkeysvalueof{/pgf/bar width},
font=\scriptsize},
]

\addplot [fill=lightgray!30] table [y index=2, col sep=semicolon]  {gruen.dat};
\addplot [fill=lightgray!110, nodes near coords] table [y index=1, col     sep=semicolon] {gruen.dat};
\legend{BWI 2002, BWI 2012}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

• Thank you very much. In the original file (beamer presentation) I do not have enough space to increase the bar width. I am sorry that I did not mention that. – Felix Phl Mar 5 '18 at 18:55
• why then in mwe you not use beamer document class? – Zarko Mar 5 '18 at 19:07
• i adopted my answer to use in beamer. now is see that your question is misleading ... – Zarko Mar 5 '18 at 19:22