pgfplots 中坐标附近的条件节点

pgfplots 中坐标附近的条件节点

我有一个matrix plot包含nan元值的。现在我想只打印nodes near coords不存在的nan,但不知为何我不知道该怎么做。

以下是 MWE:

\documentclass[tikz, border=1cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{ifthen}
\pgfplotsset{width=7cm,compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false,
point meta rel=per plot, 
nodes near coords={%
    \ifthenelse{\expandafter\equal{\pgfmathprintnumber\pgfplotspointmeta}{NaN}}{}{\pgfmathprintnumber\pgfplotspointmeta}}]
\addplot [matrix plot,
point meta=explicit,
] coordinates {
(0,0) [0] (1,0) [1] (2,0) [2]

(0,1) [nan] (1,1) [nan] (2,1) [nan]

};
\end{axis}
\end{tikzpicture}
\end{document} 

谢谢你的帮助!

答案1

扩展这个很好的答案,您可以针对相关值的标志发送文本:

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm, compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    enlargelimits=false,
    point meta rel=per plot, 
    nodes near coords={%
        \pgfmathfloatparsenumber{\pgfplotspointmeta}
        \pgfmathfloatgetflagstomacro\pgfmathresult\currentflags
        \ifnum\currentflags=3\else
            \pgfmathprintnumber\pgfplotspointmeta
        \fi
    }
]
\addplot[
    matrix plot,
    point meta=explicit,
] coordinates {
    (0,0) [0] (1,0) [1] (2,0) [2]
    
    (0,1) [nan] (1,1) [nan] (2,1) [nan]
};
\end{axis}
\end{tikzpicture}
\end{document} 

在此处输入图片描述

相关内容