12

I have a file with measurement results for different settings (group) in a single csv file as shown below. I would like to plot a line in pgfplots for each group and provide an appropriate legend. Is there a way to automate the process shown below? That is, automatically recognizing which groups exist in the data and adding a line as well as a legend entry for each group.

\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
group, x,  y 
1.10,  1,  3.5
1.10,  2,  2.2
1.10,  3,  2.1
1.10,  4,  1.5
2.12,  1,  4
2.12,  2,  3
2.12,  3,  2
2.12,  4,  1
3.21,  1,  2
3.21,  2,  2.2
3.21,  3,  2.4
3.21,  4,  2.6
\end{filecontents*}


\begin{document}
\begin{tikzpicture}

\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{axis}
\addplot table [discard if not={group}{1.10}, x=x, y=y, col sep=comma] {data.csv};
\addplot table [discard if not={group}{2.12}, x=x, y=y, col sep=comma] {data.csv};
\addplot table [discard if not={group}{3.21}, x=x, y=y, col sep=comma] {data.csv};
\legend{1,2,3}
\end{axis}
\end{tikzpicture}
\end{document}

MWE

10
  • It already looks automated to me. What would be the extra bit?
    – percusse
    Sep 2, 2015 at 9:16
  • oh, and this has nothing to do with a csv formatted file, I have removed the tag (csv=comma separated)
    – nickpapior
    Sep 2, 2015 at 9:39
  • 1
    I hope the edit clarifies it a little bit. I have also added commas.
    – koalo
    Sep 2, 2015 at 9:56
  • For others information, the scatter/classes method will currently not plot lines in their respective class system. Hence that answer is not viable.
    – nickpapior
    Sep 2, 2015 at 10:11
  • 2
    @koalo did you find a solution for this? I have exactly the same issue: given a group column in my data file add a separate plotted line for each unique value of the group column. Sounds like something a \foreach plus filtering could do but I don't know how. Feb 28, 2017 at 17:43

1 Answer 1

5

Maybe a \foreach?

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \foreach\g in{1.10,2.12,3.21}{
                \addplot table [discard if not={group}{\g}, x=x, y=y, col sep=comma] {data.csv};
            }
            \legend{1,2,3}
        \end{axis}
    \end{tikzpicture}
\end{document}

5
  • That is a good first step, but you still have to specify every group explicitly.
    – koalo
    Mar 29, 2017 at 6:00
  • @Koalo Good point. I guess there is a method to obtain a non-repeated list of entries that shows up in an pgfplotstable. If not, you can ask a new one!
    – Symbol 1
    Mar 29, 2017 at 21:08
  • 6 years later... is the a way to make this work if the value used in \ifx is not numeric? So instead of 1.10 something like example.com?
    – Yan Foto
    Jan 7, 2023 at 22:49
  • 1
    @YanFoto Probably an easier way is to generate a new table using some filter syntax. Either pgfplotstable has it or you do it yourself. Either way, it's a good idea to ask a standalone question.
    – Symbol 1
    Jan 8, 2023 at 0:30
  • @Symbol1 cheers for the quick response. I ended up adding a new numerical column that denotes each string group. I'm reluctant to "one table per group" solution since I'm going to have non-constant number of groups (depending on my data).
    – Yan Foto
    Jan 8, 2023 at 21:30

You must log in to answer this question.

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