pgfplots:如果值为 0(空),如何隐藏“坐标附近的节点”?

pgfplots:如果值为 0(空),如何隐藏“坐标附近的节点”?

假设我们有这个最小工作示例(MWE)

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}

        \begin{axis}[
            ybar,
            xtick                   = data,
            ymin                    = 0,
            symbolic x coords       = {Bar 1, Bar 2, Bar 3},
            nodes near coords,
            ]

            \addplot coordinates {(Bar 1, 50) (Bar 2, 0) (Bar 3, 50)};%
        \end{axis}

    \end{tikzpicture}
\end{document}

结果截图:

结果截图


问题说明:

如您所见,中间有一些难看的“0” 。如果它们的对应值为,ybar我该如何避免/隐藏它们,以便显示的数字应该是不可见的?nodes near coords0

答案1

可能有点绕弯子,但这似乎有效:

输出

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}

        \begin{axis}[
            ybar,
            xtick                   = data,
            ymin                    = 0,
            symbolic x coords       = {Bar 1, Bar 2, Bar 3},
            visualization depends on={y\as\YY},
            nodes near coords={\pgfmathtruncatemacro{\YY}{ifthenelse(\YY==0,0,1)}\ifnum\YY=0\else\pgfmathprintnumber\pgfplotspointmeta\fi},
            ]

            \addplot coordinates {(Bar 1, 50) (Bar 2, 0) (Bar 3, 50)};%
        \end{axis}

    \end{tikzpicture}
\end{document}

答案2

我正在做一些类似的事情,但是-->在逻辑测试中我遇到了困难(没有成功)。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{xifthen}

% Instiration
% https://tex.stackexchange.com/questions/36114
% https://tex.stackexchange.com/questions/275187

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
        ybar,
        xtick = data,
        ymin = 0,
        symbolic x coords = {Bar 1, Bar 2, Bar 3},
        visualization depends on={rawy \as \myRawy},
        nodes near coords = {
        \ifthenelse{<Logic Test>} % something like '\myRawy < 0.001'
            {<True Case>} % Do nothing
            {<False Case>} % Print \myRawy
        },
        ]
        \addplot coordinates {(Bar 1, 50) (Bar 2, 0) (Bar 3, 50)};
    \end{axis}
\end{tikzpicture}
\end{document}

也许这里有人可以优雅地解决这个问题。

相关内容