2

I can plot data depending on the value in one column like this:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{data.csv}
a,b,c,d
0,0,1,2
2,2,1,2
3,-3,1,3
2,1,2,4
\end{filecontents}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
  \addplot+[only marks]
    table[x=a, y=b, col sep=comma,
      restrict expr to domain={\thisrow{c}}{1:1}]{data.csv};
  \end{axis}
\end{tikzpicture}
\end{document}

Is it possible to apply two conditions? For example, I would like to plot data where both the value in c = 1 and the value in d = 2 without plotting data where only one applies.

3

You can use x expr to apply the conditions. If the result is not true, set the x value to nan:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{data.csv}
a,b,c,d
0,0,1,2
2,2,1,2
3,-3,1,3
2,1,2,4
\end{filecontents}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
  \addplot+[only marks]
    table[
      x expr={and(\thisrow{c}==1,\thisrow{d}==2)==1?\thisrow{a}:nan},
      y=b,
      col sep=comma,
      ]{data.csv};
  \end{axis}
\end{tikzpicture}
\end{document}

Result:

enter image description here

1
  • Perfect. And for anyone wanting three conditions, this seems to work: x expr={and(\thisrow{c}==1,and(\thisrow{a}==0,\thisrow{d}==2))==1?\thisrow{a}:nan}, Jan 28 '18 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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