14

I have a data file which contains all the experiments that have been conducted for different cases. Is it possible in pgfplots to only read specific portion of the file and plot the results?

Let's say the data file, named mydata.dat, looks like the following:

case    var1    var2    var3
1   0.10    0.25    10.00
1   0.20    0.35    25.00
1   0.30    0.45    40.00
1   0.40    0.55    55.00
1   0.50    0.65    70.00
1   0.60    0.75    85.00
2   1.00    0.50    55.00
2   2.00    0.50    55.00
2   3.00    0.50    55.00
2   4.00    0.50    55.00
2   5.00    0.50    55.00
2   6.00    0.50    55.00
2   7.00    0.50    55.00
2   8.00    0.50    55.00
2   9.00    0.50    55.00
3   10.00   1.00    22.50
3   20.00   2.00    22.50
3   30.00   3.00    22.60
3   40.00   4.00    22.70
3   50.00   5.00    22.65
4   100.00  10.00   135.00
4   200.00  20.00   140.00
4   300.00  30.00   137.00
4   400.00  40.00   144.00
4   500.00  50.00   141.00
4   600.00  60.00   136.00
4   700.00  70.00   136.00

How can I read the file using pgfplot and for example plot the data only for case=3 which are:

3   10.00   1.00    22.50
3   20.00   2.00    22.50
3   30.00   3.00    22.60
3   40.00   4.00    22.70
3   50.00   5.00    22.65

How can I modify the following code to be able to obtain the results I am looking for?

\documentclass[crop=true,border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{graphicx}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[%
        axis on top,
        width = 4cm,
        height = 4cm,
        scale only axis,           
    ]
    \addplot
        table [%
        col sep=tab, %
        x=var1, y=var2,] %
        {mydata.dat};
    \end{axis}
\end{tikzpicture}

\end{document}
1
  • As you've noted yourself, filtering is probably the way to go, but I do notice that your var1 column here has ranges that suggest you could simple do xmax/xmin setting.
    – Joseph Wright
    May 17, 2014 at 5:45

2 Answers 2

8

I found the solution by adding the following code, taken from Is it possible to change the color of a single bar when the bar plot is based on symbolic values?

\pgfplotsset{
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

It defines a filter such that every row of the given column name (here parameterized by the first input #1) is compared with the value given by the input parameter #2 and if the result is false then the row value is set to infinity, otherwise the value of \pgfmathresult is passed through. Same functionality can also be achieved via PGF math comparison

\pgfplotsset{
    discard if not/.style 2 args={
        x filter/.code={%
            \let\tempa=\pgfmathresult% Save the original value
            \pgfmathparse{\thisrow{#1}==#2?\tempa:"inf"}% Test against #2
        }
    }
}

This solution relies on the unbounded coords=discard setting such that whenever the value inf is found that coordinate set is discarded.

Here is the working code:

\documentclass[crop=true,border=2mm]{standalone}
\usepackage{pgfplots}


\begin{document}

\pgfplotsset{
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

\begin{tikzpicture}
  \begin{axis}[
      title={{\bfseries Plot for case3}},
      xlabel={var1},
      ylabel={var2}]
    \addplot+[only marks, discard if not={case}{3}] table[x=var1, y=var2]
      {mydata.dat};
  \end{axis}
\end{tikzpicture}


\end{document}

enter image description here

5
  • 1
    Added some comments.
    – percusse
    May 17, 2014 at 1:59
  • @percusse: Thank you. Does discard if not/.style 2 args define the command name as discard if not and specifies that this command needs two arguments? I would be thankful if you could also let me know the role of /.style?
    – afp_2008
    May 17, 2014 at 2:28
  • Yes. You can find those details in the TikZ manual in the section of pgfkeys
    – percusse
    May 17, 2014 at 2:29
  • Visit tex.stackexchange.com/q/178652/23594 for the interpretation of the added pgfplotsset.
    – afp_2008
    May 17, 2014 at 8:23
  • @A2009: One note about the statement that "same functionality can also be achieved via PGF math comparison": the PGF math comparison can be used for numerical comparisons, where you want 1.00 == 1 to return True. The approach with \ifx is used for comparing symbolic (i.e. text) coordinates. If I remember correctly, the code was first used in [tex.stackexchange.com/questions/58548/… it possible to change the color of a single bar when the bar plot is based on symbolic values?).
    – Jake
    May 17, 2014 at 9:23
0

Here is a shorter alternative:

\addplot table [col sep=tab, x=var1, %
                y expr={\thisrow{var2} / (\thisrow{case} == 3)}] %
      {mydata.dat};

It is based on operator == which returns either 1 or 0. When it returns 0, the division gives infinity and the corresponding row is not plotted.

Same logic can be used with < and > operators to plot specific range of values.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .