2

I want to filter and plot my data by two criteria. First I want to filter the data by column Test and then by Test No. and then plot it. Unfortunately, I am getting nowhere with the filtering. I tried it directly with the package pgfplots. After that I tried the package pgfplotstable, but here I failed as well.

The following posts did not help me:

My result:

\documentclass[a4paper,12pt,headsepline]{scrartcl}
\usepackage{tikz,pgfplots,siunitx,pgfplotstable}
\usepgfplotslibrary{units}
\pgfplotsset{compat=1.18}
\usepackage{filecontents}
%\begin{filecontents}{data.csv}
%Test No.;Test;time [s];u [ms]
%A1;A;3;0.045
%A2;A;5;0.06
%A2;A;4;0.05
%A2;A;3;0.04
%A2;A;2;0.03
%A2;A;1;0.02
%A2;A;0;0.01
%A3;A;3;0.044
%B1;B;10;0.045
%B1;B;20;0.06
%B1;B;30;0.05
%B1;B;40;0.04
%B1;B;50;0.03
%B1b;B;10;0.02
%B1b;B;20;0.01
%B1b;B;30;0.044
%B1b;B;40;0.045
%B1b;B;50;0.06
%C1;C;10;0.05
%C1;C;20;0.04
%C1;C;30;0.03
%C1;C;40;0.02
%C1;C;50;0.01
%C1;C;60;0.044
%C1;C;70;0.8
%C2;C;60;0.4
%\end{filecontents}


\begin{document}


\begin{tikzpicture}
    \begin{axis}
    [
        width=\linewidth,
        grid=major,
        grid style={dashed,gray!30},
        xlabel=$time$,
        ylabel=$u$,
        x unit=\si{\second},
        y unit=\si{\meter\per\second}
    ]
    \addplot table [x={time [s]}, y={u [ms]}, col sep=semicolon] {data.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

Data in one plot

My solution

Thank you @samcarter_is_at_topanswers.xyz for your answer. I just updated the plot and addded some curves if s/o wants to plot more graphs in one figure.

\documentclass[a4paper,12pt,headsepline]{scrartcl}
\usepackage{tikz,pgfplots,siunitx,pgfplotstable}
\usepgfplotslibrary{units}
\pgfplotsset{compat=1.18}
\usepackage{filecontents}
\begin{filecontents}{data.csv}
Test No.;Test;time [s];u [ms]
A1;A;3;0.045
A2;A;5;0.06
A2;A;4;0.05
A2;A;3;0.04
A2;A;2;0.03
A2;A;1;0.02
A2;A;0;0.01
A3;A;3;0.044
B1;B;10;0.045
B1;B;20;0.06
B1;B;30;0.05
B1;B;40;0.04
B1;B;50;0.03
B1b;B;10;0.02
B1b;B;20;0.01
B1b;B;30;0.044
B1b;B;40;0.045
B1b;B;50;0.06
C1;C;10;0.05
C1;C;20;0.04
C1;C;30;0.03
C1;C;40;0.02
C1;C;50;0.01
C1;C;60;0.044
C1;C;70;0.8
C2;C;60;0.4
\end{filecontents}

\usepackage{xcolor}
\usepackage{xstring}

\begin{document}



\begin{tikzpicture}
    \begin{axis}
    [
        width=\linewidth,
        grid=major,
        grid style={dashed,gray!30},
        xlabel=$time$,
        ylabel=$u$,
        x unit=\si{\second},
        y unit=\si{\meter\per\second},    
    ]
    \addplot [color=blue,
        mark=o,
        x filter/.code={
        \IfStrEq{\thisrow{Test}}{C}{
            \IfStrEq{\thisrow{Test No.}}{C1}{}{\def\pgfmathresult{}}
        }{\def\pgfmathresult{}}
        }, ] table [x={time [s]}, y={u [ms]}, col sep=semicolon] {data.csv};
    \addplot [color=red, 
        mark=*,
        x filter/.code={
        \IfStrEq{\thisrow{Test}}{B}{
            \IfStrEq{\thisrow{Test No.}}{B1b}{}{\def\pgfmathresult{}}
        }{\def\pgfmathresult{}}
        }, ] table [x={time [s]}, y={u [ms]}, col sep=semicolon] {data.csv};
    \addplot [color=green,
        mark=x,
        x filter/.code={
        \IfStrEq{\thisrow{Test}}{A}{
            \IfStrEq{\thisrow{Test No.}}{A2}{}{\def\pgfmathresult{}}
        }{\def\pgfmathresult{}}
        }, ] table [x={time [s]}, y={u [ms]}, col sep=semicolon] {data.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Kuba1623
  • 109
  • 6

1 Answers1

3

You don't tell what you want to filter, but here an example that first checks that Test equals C and then that Test No. is C1:

\documentclass[a4paper,12pt,headsepline]{scrartcl}
\usepackage{tikz,pgfplots,siunitx,pgfplotstable}
\usepgfplotslibrary{units}
\pgfplotsset{compat=1.18}
\usepackage{filecontents}
\begin{filecontents}{data.csv}
Test No.;Test;time [s];u [ms]
A1;A;3;0.045
A2;A;5;0.06
A2;A;4;0.05
A2;A;3;0.04
A2;A;2;0.03
A2;A;1;0.02
A2;A;0;0.01
A3;A;3;0.044
B1;B;10;0.045
B1;B;20;0.06
B1;B;30;0.05
B1;B;40;0.04
B1;B;50;0.03
B1b;B;10;0.02
B1b;B;20;0.01
B1b;B;30;0.044
B1b;B;40;0.045
B1b;B;50;0.06
C1;C;10;0.05
C1;C;20;0.04
C1;C;30;0.03
C1;C;40;0.02
C1;C;50;0.01
C1;C;60;0.044
C1;C;70;0.8
C2;C;60;0.4
\end{filecontents}

\usepackage{xstring}

\begin{document}



\begin{tikzpicture}
    \begin{axis}
    [
        width=\linewidth,
        grid=major,
        grid style={dashed,gray!30},
        xlabel=$time$,
        ylabel=$u$,
        x unit=\si{\second},
        y unit=\si{\meter\per\second},
        x filter/.code={
          \IfStrEq{\thisrow{Test}}{C}{
            \IfStrEq{\thisrow{Test No.}}{C1}{}{\def\pgfmathresult{}}
          }{\def\pgfmathresult{}}
        },     
    ]
    \addplot table [x={time [s]}, y={u [ms]}, col sep=semicolon] {data.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here