4

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}]
        \addplot coordinates {(1,5) (2,7) (3,9)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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

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}
        },
        % -----------------------------------------------------------------
    ]
        \addplot coordinates {(1,5) (2,7) (3,10)};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • +1: Perfect! So rawy is a predefined variable as it looks like? – Dr. Manuel Kuehner Jun 11 at 5:15
  • 1
    @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. – Stefan Pinnow Jun 11 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. – Keelan Jun 11 at 18:41
4

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
            ]
        \addplot coordinates {(1,5) (2,7) (3,9)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here


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            
            },
            ]
        \addplot coordinates {(1,5) (2,7) (3,9)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • 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? – Keelan Jun 10 at 12:41
  • @Keelan clip applies to the axis environment I guess. – Dr. Manuel Kuehner Jun 10 at 12:53
  • @marmot Is this a case for you? :) – Dr. Manuel Kuehner Jun 10 at 13:02
3

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);
        \addplot coordinates {(1,5) (2,7) (3,9)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • +1: Thanks for the support, any ideas why my proposal ("Update 1") does not work as expected? – Dr. Manuel Kuehner Jun 10 at 14:16
  • 1
    @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. – user121799 Jun 10 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.