pgfplots:如何检查 addplot 是否过滤所有数据

pgfplots:如何检查 addplot 是否过滤所有数据

有没有一种简单的方法来检查 PGFPLOTS 中的 \addplot(+) table [...] 命令是否绘制了任何点?原因是我想动态处理数据,如果图为空,则应省略图例。

以下是 MWE:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfkeys{
        /tr/rowfilter/.style 2 args={
                /pgfplots/x filter/.append code={
                        \edef\arga{\thisrow{#1}}
                        \edef\argb{#2}
                        \ifx\arga\argb
                        \else
                                \def\pgfmathresult{}
                        \fi
                }
        }
}

\usepackage{filecontents}
\begin{filecontents}{diagram.dat}
x y kind
1 10 a
1 20 a
2 13 b
2 15 b
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\begin{axis}[filter discard warning=false]
    \addplot table[/tr/rowfilter={kind}{a},/tr/rowfilter={x}{2}] {diagram.dat}; % if the plot doesn't contain curve a,
    \addlegendentry{curve a}                                                    % <- don't execute this line
    \addplot table[/tr/rowfilter={kind}{b},/tr/rowfilter={x}{2}] {diagram.dat};
    \addlegendentry{curve b}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[filter discard warning=false]
    \addplot table[/tr/rowfilter={kind}{a},/tr/rowfilter={x}{1}] {diagram.dat};
    \addlegendentry{curve a}
    \addplot table[/tr/rowfilter={kind}{b},/tr/rowfilter={x}{2}] {diagram.dat};
    \addlegendentry{curve b}
\end{axis}
\end{tikzpicture}

\end{document}

第一个图中的图例是错误的:该线属于曲线 b 的数据集!我想到了 \newif\ifplotexists 之类的东西,但是,由于有两个过滤器,如何决定是否应设置 \plotexistsfalse 或 \plotexiststrue?

答案1

如果在选项中提供\addlegendentry使用,则仅当图表未完全过滤掉时才会添加图例条目:execute at end plot visualization=...\addplot

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfkeys{
        /tr/rowfilter/.style 2 args={
                /pgfplots/x filter/.append code={
                        \edef\arga{\thisrow{#1}}
                        \edef\argb{#2}
                        \ifx\arga\argb
                        \else
                                \def\pgfmathresult{}
                        \fi
                }
        }
}

\usepackage{filecontents}
\begin{filecontents}{diagram.dat}
x y kind
1 10 a
1 20 a
2 13 b
2 15 b
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\begin{axis}[filter discard warning=false]
    \addplot +[execute at end plot visualization=\addlegendentry{A}] table[/tr/rowfilter={kind}{a},/tr/rowfilter={x}{2}] {diagram.dat}; % if the plot doesn't contain curve a,                                               % <- don't execute this line
    \addplot +[execute at end plot visualization=\addlegendentry{B}] table[/tr/rowfilter={kind}{b},/tr/rowfilter={x}{2}] {diagram.dat};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[filter discard warning=false]
    \addplot +[execute at end plot visualization=\addlegendentry{A}] table[/tr/rowfilter={kind}{a},/tr/rowfilter={x}{1}] {diagram.dat};
    \addplot  +[execute at end plot visualization=\addlegendentry{B}] table[/tr/rowfilter={kind}{b},/tr/rowfilter={x}{2}] {diagram.dat};
\end{axis}
\end{tikzpicture}

\end{document}

相关内容