# How to conditionally specify the options for nodes near coords in pgfplots?

Now I'm plotting some ybar figures using pgfplots. I want to show the values on the ybar. However, some values are too small and I don't want to show them. The following is an example

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
nodes near coords,
% nodes near coords*={
%     \ifnum \pgfplotspointmeta>0.1
%         \pgfmathprintnumber{\pgfplotspointmeta}
%     \fi
% },
]
\addplot table {
1 0.5
2 0.8
3 0.02
};
\addplot table {
1 0.5
2 0.2
3 0.98
};
\end{axis}
\end{tikzpicture}
\end{document}

In the above code, the ybar contains two segments. The bottom bar at x=3 is very small, and I don't want to display it. I want to use conditional option for nodes near coords as commented in the above code (the values less than 0.1 should not display), but it failed. I want to know the reason and how can I realize what I want.

• It would be a lot easier just to do it manually than try to automate it. Note: missing (blank) data is not plotted. Jun 10 at 12:44
• @JohnKormylo Thanks for your comment. I thought it is a easy thing for automating it, but actually I didn't find the suitable method to modify the code for automation. Jun 10 at 14:53

## 2 Answers

Here an automated solution for your question.

% used PGFPlots v1.18.1
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% use this compat level or higher so the new default that nodes near coords
% are placed in the middle of the bars instead of over the top is used
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
nodes near coords,
% -----------------------------------------------------------------
% we create a style for the nodes near coords which is dependent
% on the value
% (adapted from <http://tex.stackexchange.com/a/141006/95441>)
% (#1: the THRESHOLD after which we switch to a special display)
nodes near coords greater equal only/.style={
% define the style of the nodes with "small" values
small value/.style={
/tikz/coordinate,
},
every node near coord/.append style={
check for small values/.code={
\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}
\fi
},
check for small values,
},
},
% assign a value to the new style which is the threshold at which
% the small value style is used.
nodes near coords greater equal only=0.2,
% -----------------------------------------------------------------
]
\addplot table {
1 0.5
2 0.8
3 0.02
};
\addplot table {
1 0.5
2 0.2
3 0.98
};
\end{axis}
\end{tikzpicture}
\end{document}

• Thanks for your answer. I thought it is a simple problem, but actually the answer is beyond my expection, it is a bit complicated for me. Jun 12 at 10:47
• I also don't understand everything in the answer myself, i.e. all the details in check for small values/.code. But that is not necessary. One only needs to know what this does in total. Then you can modify the parts around that to satisfy your needs. Here two more examples doing similar stuff with similar code. If you compare then I think you find out what does what and will also be able to adapt it to your next needs ;) tex.stackexchange.com/a/364729/95441 and tex.stackexchange.com/a/349619/95441 Jun 12 at 12:12

As specified in the documentation (page 115, see example below), you can use a function to decide what to print near the coordinates. I guess you could use that for your purpose.

In your case, you just need an else case, I think.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.17}
\begin{document}
\begin{tikzpicture}\begin{axis}[enlargelimits=0.2]
\addplot[scatter,mark=*,only marks,% we use 'point meta' as color data...
point meta=\thisrow{color},
% ... therefore, we can't use it as argument for nodes near coords ...
nodes near coords*={
\pgfmathapproxequalto{\myvalue}{0.25}
% standard TeX:
\ifpgfmathcomparison
true%
\else
false%
\fi
},% ... which requires to define a visualization dependency:
visualization depends on={\thisrow{myvalue} \as \myvalue},
] table{
x   y   color   myvalue
0.5    0.2  1   0.25
0.2    0.1  2   1.5
0.7    0.6  3   0.75
0.35   0.4  4   0.125
0.65   0.1  5   2
};
\end{axis}
\end{tikzpicture}
\end{document}