[Tex/LaTex] How to filter/select data from a table and plot it

pgfplotspgfplotstable

I have a table with data from a file and before anything, I sort the data. Then I would like to filter only the rows that have a specific value. There has been a similar question which almost contains the answer to my question:

Selecting rows to be displayed with pgfplotstable

In addition to that I would like to know how to plot the filtered data.

Here is an example:

\documentclass{minimal}
\usepackage{pgfplotstable} 
\usepackage{pgfplots}   

\begin{document}
\pgfplotstableread{
T   A   B   C
20  0   450 43
23  0   400 42
25  0   350 41
30  0   320 40
40  0   300 40
20  10  400 38
23  10  380 37
25  10  350 36
30  10  310 35
40  10  280 34
}\data

\pgfplotstablesort[sort key={T}]{\sorted}{\data} %get the data and sort by column 'T'

   %select data in column 'T' that complies with a certain value and write the table
   \pgfplotstabletypeset[row predicate/.code={%
   \pgfplotstablegetelem{#1}{T}\of{\sorted}
   \ifnum\pgfplotsretval=20\relax
   \else\pgfplotstableuserowfalse\fi}]
   {\sorted}

   \begin{tikzpicture}
    \begin{axis}[
     xlabel=A,
     ylabel=B]
   \addplot table[
    x=A,
    y=B]
   {\filtered}; %here the filtered data from above needs to be addressed, how to do that?
   \end{axis}
   \end{tikzpicture}

   \end{document}

What I don't know is how to address the filtered data above to plot it. I thought it might work by generating a table named \filtered.

Best Answer

You can use the x filter/.code={} instructions (thanks to Jake for spotting a mistake)

\documentclass{minimal}
\usepackage{pgfplotstable} 
\usepackage{pgfplots}   

\begin{document}
\pgfplotstableread{
T   A   B   C
20  0   450 43
23  0   400 42
25  0   350 41
30  0   320 40
40  0   300 40
20  10  400 38
23  10  380 37
25  10  350 36
30  10  310 35
40  10  280 34
}\data

\pgfplotstablesort[sort key={T}]{\sorted}{\data}

   \pgfplotstabletypeset[row predicate/.code={%
   \pgfplotstablegetelem{#1}{T}\of{\sorted}
   \ifnum\pgfplotsretval=20\relax
   \else\pgfplotstableuserowfalse\fi}]
   {\sorted}


   \begin{tikzpicture}
    \begin{axis}[
     xlabel=A,
     ylabel=B,
    x filter/.code={\pgfplotstablegetelem{\coordindex}{T}\of{\sorted}
                    \ifnum\pgfplotsretval=20
                    \else
                    \def\pgfmathresult{}
                    \fi
                   },
    ]
   \addplot[only marks] table[x=A,y=B] {\sorted};
   \end{axis}
   \end{tikzpicture}
   \end{document}

enter image description here

If the entries of the table involves nonintegers then \ifnum would complain hence it needs another step.

\documentclass{minimal}
\usepackage{pgfplotstable} 
\usepackage{pgfplots}   

\begin{document}
\pgfplotstableread{
T   A   B   C
0.567641034 0.111435797 0.886143166 0.536835288
0.919931469 0.702921973 0.285814111 0.796042178
0.073058973 0.921420655 0.806947507 0.570962079
0.697073289 0.017461888 0.313614435 0.905702895
0.536575411 0.814853755 0.152870278 0.556116015
0.574615343 0.162798271 0.553041948 0.605668041
0.567641034 0.248224272 0.448683336 0.092672169
0.117628849 0.115555917 0.193712749 0.561100742
0.625852254 0.993854404 0.950034992 0.595500664
0.975451837 0.152198913 0.647432196 0.790471281
0.692695418 0.099750965 0.122178502 0.141174403
0.106974613 0.905644903 0.491119067 0.795346
0.727907584 0.408384001 0.586645737 0.172061294
}\data

\pgfplotstablesort[sort key={T}]{\sorted}{\data}



   \begin{tikzpicture}
    \begin{axis}[
     xlabel=A,
     ylabel=B,
    x filter/.code={\pgfplotstablegetelem{\coordindex}{T}\of{\sorted}
                    \pgfmathtruncatemacro{\tempva}{abs(\pgfplotsretval - 0.567641034) == 0 ? 1: 0}
                    \ifnum\tempva>0%true
                    \else%false
                    \def\pgfmathresult{}
                    \fi
                   },
    ]
   \addplot[only marks] table[x=A,y=B] {\sorted};
   \end{axis}
   \end{tikzpicture}
   \end{document}

which leads to the same output above. I have modified a table entry as it is almost passing the test if lowered slightly. So you should be careful about the numerical precision.