# pgfplots: how to check if addplot filters all data

is there an easy way to check if a \addplot(+) table [...] command in PGFPLOTS has any drawn any points? The reason is that I want to process the data dynamically, and if the plot is empty, the legend should be omitted.

Here is a 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}


In the first diagram the legend is wrong: the line belongs to dataset of curve b! I thought of something like \newif\ifplotexists, but, as there are two filters, how to decide if \plotexistsfalse or \plotexiststrue should be set?

-

## 1 Answer

If you supply \addlegendentry using execute at end plot visualization=... in the \addplot options, the legend entry will only be added if the plot is not completely filtered away:

\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}

-
thanks, this easier than I imagined:) do you know why if-commands created with \newif don't work with 'execute at end plot visualization'? –  Thomas Rebele Aug 17 '12 at 23:20
@ThomasRebele: Hm, because of a syntax error? =) I'd suggest you open a new question with an example. –  Jake Aug 18 '12 at 7:52
no, it doesn't produce a syntax error! ^^ it simply doesn't work as expected. I'll put that better in a new question, maybe next week :) –  Thomas Rebele Aug 19 '12 at 12:55