# How to show a "node near coord" even when it is out of bounds (with clip = true)?

I have a ybar graph using the pgfplots package in which I use nodes near coords to display y values near the tops of the bars. There are some outliers in the data, for which I'd like to show a bar filled to the top of the graph. In this case, the y value should be displayed in the middle of the bar. I have tried to use visualization depends on to set a different yshift for the nodes near the coordinates for these outliers. However, it seems that pgfplots simply does not draw these nodes at all, because the node is still not displayed even with this yshift.

The below example clarifies this. Here I shift the nodes for values larger than 6. The first value is correctly placed above the bar; the second value is correctly shifted down. However, the last value ((3,9)) does not appear at all. How can I get this node to appear as well?

NB: the value for \myshift here is not finalized yet; I haven't found a good way yet to calculate this appropriately. Suggestions welcome.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,ymax=8,
xmin=0,xmax=4,
nodes near coords,
visualization depends on={ifthenelse(y>6,-y,0) \as \myshift},
every node near coord/.append style={yshift=\myshift em}]
\end{axis}
\end{tikzpicture}
\end{document}


• Not related: Choose article or standalone instead of \documentclass{minimal}. minimal can cause additional trouble. article is the true "minimal" document class :). Jun 10, 2019 at 12:22
• @Dr.ManuelKuehner thanks, didn't know about that! Jun 10, 2019 at 12:23

Not exactly what you were asking for, but I think this is a better alternative than shifting the nodes near coords to the middle of the bar. Instead I still write it on top of it, although it is "truncated".

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
xmin=0,
xmax=4,
ymin=0,
ymax=8,
xtick=data,
% -----------------------------------------------------------------
% we store the *original* y value in a variable ...
visualization depends on={rawy \as \rawy},
% ... which value should be shown in the nodes near coords' ...
nodes near coords={\pgfmathprintnumber\rawy},
% ... and we want to limit the range of the bars to the axis limits
restrict y to domain*={
\pgfkeysvalueof{/pgfplots/ymin}:\pgfkeysvalueof{/pgfplots/ymax}
},
% -----------------------------------------------------------------
]
\end{axis}
\end{tikzpicture}
\end{document}


• +1: Perfect! So rawy is a predefined variable as it looks like? Jun 11, 2019 at 5:15
• @Dr.ManuelKuehner, many thanks. Yes, rawx and friends can be found in various contexts. This is most likely the case when transformation of the raw data is involved. Jun 11, 2019 at 5:24
• Thanks very much, this looks like a neater solution indeed! I decided to put the label at the top of the bar but inside the axis. I'm not sure what it translates to for the MWE, but in my larger example this is anchor={\ifdim\myy pt=0pt west\else east\fi}. You are right that placing them in the middle of the bar was a bad idea. Jun 11, 2019 at 18:41

Maybe clip = false is what you are looking for.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin = 0,
ymax = 8,
xmin = 0,
xmax = 4,
nodes near coords,
clip = false, % <--- Added
]
\end{axis}
\end{tikzpicture}
\end{document}


## Update 1

This almost works -- if I use clip = true then the 9 is not printed anymore at the bottom (strangely).

Maybe some of the experts here can support.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

% Based on
% https://tex.stackexchange.com/questions/110879

\begin{tikzpicture}
\begin{axis}[
ybar,
ymin = 0,
ymax = 8,
xmin = 0,
xmax = 4,
nodes near coords,
clip = false, % <--- Added, true does not work
visualization depends on = y \as \myYOffset,  % <--- Added
every node near coord/.append style = {
shift = { (axis direction cs: 0, -\myYOffset) } % <--- Added
},
]
\end{axis}
\end{tikzpicture}
\end{document}


• Thanks, that's almost what I'm looking for. I'd still like the bar to be clipped however. Unfortunately adding clip=false to every node coords/.append style instead does not work (same result as my original code). Any ideas? Jun 10, 2019 at 12:41
• @Keelan clip applies to the axis environment I guess. Jun 10, 2019 at 12:53
• @marmot Is this a case for you? :) Jun 10, 2019 at 13:02

Very similar to Dr. Manuel Kuehner's answer except that a clip for the plots is added by hand.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[clip=false,
ybar,
ymin=0,ymax=8,
xmin=0,xmax=4,
nodes near coords,
visualization depends on={y \as \myy},
nodes near coords style={at={(0,-\myy/2)}}]
\clip (current axis.south west) rectangle (current axis.north east);

• @Dr.ManuelKuehner This is most likely related to the way pgfplots` clips. It is more than the usual pgf clip, see section 4.20.2 Clipping of the manual.