# pgfplots: nodes near coords placement

I have bar chart where my node labels are overflowing. What is the best way to solve this?

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
title=sample count per class,
xbar, xmin=0,
width=8cm, height=6cm, enlarge y limits=0.15,
symbolic y coords={{,},{.},{-},{:}, {?}},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
point meta=rawx,
nodes near coords style={/pgf/number format/.cd,fixed},
]
\addplot [xbar] coordinates {
(1290282,{.})
(2208970,{,})
(81710,{-})
(51192,{:})
(40511,{?})
};
\end{axis}
\end{tikzpicture}

\end{document}


## 2 Answers

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
title=sample count per class,
xbar, xmin=0,xmax=3000000,
width=8cm, height=6cm, enlarge y limits=0.15,
symbolic y coords={{,},{.},{-},{:}, {?}},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
point meta=rawx,
nodes near coords style={/pgf/number format/.cd,fixed},
]
\addplot [xbar] coordinates {
(1290282,{.})
(2208970,{,})
(81710,{-})
(51192,{:})
(40511,{?})
};
\end{axis}
\end{tikzpicture}

\end{document}

• That's a neat little hack and it looks great. Thank you @SebGlav May 13 at 14:24
• Sometimes a little hack is worth a complex solution. But other things are possible (like writing vertically instead of horizontally). May 13 at 15:38

Here I present an alternative, if you don't want to enlarge the ymax. For details, please have a look at the comments in the code.

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
% we need PGFPlotsTable ...
\usepackage{pgfplotstable}
% ... to store the data in an inline table
% (alternatively one could store it in a file)
\pgfplotstableread{
x       y
1290282 {.}
2208970 {,}
81710   {-}
51192   {:}
40511   {?}
}{\MyData}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title=sample count per class,
xbar, xmin=0,
width=8cm, height=6cm, enlarge y limits=0.15,
%        % this avoids the need for symbolic y coords ...
%        symbolic y coords={{,},{.},{-},{:},{?}},
% ... and instead we can load them from the table
yticklabels from table={\MyData}{y},
ytick=data,
% ---------------------------------------------------------------------
% adjusted from <https://tex.stackexchange.com/a/335742/95441>
% ---------------------------------------------------------------------
% #1: the THRESHOLD after which we switch to a special display.
nodes near coords custom/.style={
small value/.style={
% everything is fine here, so I think there is no need
% to change something
},
large value/.style={
color=white,
anchor=east,
},
every node near coord/.style={
/pgf/number format/fixed,
check for zero/.code={%
\pgfmathfloatifflags{\pgfplotspointmeta}{0}{%
% If meta=0, make the node a coordinate (which doesn't have text)
\pgfkeys{/tikz/coordinate}%
}{%
\begingroup
% this group is merely to switch to FPU locally. Might be
% unnecessary, but who knows.
\pgfkeys{/pgf/fpu}%
\pgfmathparse{\pgfplotspointmeta<#1}%
\global\let\result=\pgfmathresult
\endgroup
%
% simplifies debugging:
%\show\result
%
\pgfmathfloatcreate{1}{1.0}{0}%
\let\ONE=\pgfmathresult
\ifx\result\ONE
% AH : our condition 'y < #1' is met.
\pgfkeysalso{/pgfplots/small value}%
\else
% ok, proceed as usual.
\pgfkeysalso{/pgfplots/large value}%
\fi
}
},
check for zero,
},
},
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}},
nodes near coords custom=5e5,
% ---------------------------------------------------------------------
]
% adjusted coordinates to table
\addplot table [x=x,y expr=\coordindex] {\MyData};
\end{axis}
\end{tikzpicture}
\end{document}


• This is a great solution. I had something like this in mind, but no idea how to implement it. May 14 at 13:10
• Then I am happy that I provided it. May 14 at 14:26