Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.