Is it possible to hide nodes near coords in pgfplots when value = 0 ?

I have something like this.

enter image description here

And axis setting

  bar width=3pt,
  nodes near coords
  • 4
    It's always a good idea to provide the community with some sample data as well as a (complete) minimal working example (MWE) - something we can copy-and-paste-and-compile. It just makes it easier to work with the problem.
    – Werner
    Mar 5 '15 at 6:43

As standard node near coordinates is \pgfmathprintnumber{\pgfplotspointmeta}. You want to run that only if \pgfplotspointmeta is non-zero. This may be tested for with \pgfmathfloatifflags as follows:

near coords={\pgfmathfloatifflags{\pgfplotspointmeta}{0}{}{\pgfmathprintnumber{\pgfplotspointmeta}}}

with 0 indicating test whether the number is equal to zero, {} being the action if it is and the final group containing the action if it is not zero.

You don't provide the code for your plot, so here is a similar case instead:

Sample output




    bar width=3pt,
    nodes near coords={\pgfmathfloatifflags{\pgfplotspointmeta}{0}{}{\pgfmathprintnumber{\pgfplotspointmeta}}}
    \addplot coordinates
    {(0,3) (1,0) (2,0) (3,1) (4,2)};


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.