# PGFplot plot y over x where z=bar (comparable to SQL WHERE clause)

I am using TexLive XuaLatex with PGFplots 1.13 to plot data. I have a table like this:

 x , y  , z
0 , 0  , 0
0 , 0  , 1
1 , 1  , 0
1 , 1  , 1
2 , 2  , 0
2 , 4  , 1
3 , 3  , 0
3 , 9  , 1
4 , 4  , 0
4 , 16 , 1
5 , 5  , 0
5 , 25 , 1
6 , 6  , 0
6 , 36 , 1


How can I use the PGFplot axis-environment to plot y over x where z=bar, when bar is a number. I'm thinking kind of the way you can access data with MySQL

\begin{tikzpicture}
\begin{axis}[/pgf/number format/.cd, use comma]
\addplot
table[x=x,y=y,WHERE z=0, col sep=comma]
{DATA.csv};

\addplot
table[x=x,y=y,WHERE z=1,col sep=comma]
{DATA.csv};

\end{axis}
\end{tikzpicture}


I have not found anything on that in the guide. I do not want a solution that omits the question by having a table like that:

x,   y1,   y2


and then do plot with x=x, y=y1 and a second plot with x=x, y=y2.

This might be a handy solution for the simple example I am giving but it does not work for my problem, because in the real problem there are 10 different y to be plotted over x for 4 different z, and I really don't want the implementation like the solution proposed for several reasons.

Thank you very much for your time

• Have you looked at/tried x filter/.code (or its y/z equivalent) yet? See here: tex.stackexchange.com/a/66192/131649 or the pgfplots manual section 4.22 (p.383f in v1.14). – alpenwasser Apr 26 '17 at 18:45

## 1 Answer

I think there are several ways of doing this. One way would be to use y expr={ifthenelse(\thisrow{z}==0,\thisrow{y},nan} instead of y=y.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepackage{filecontents}
\begin{filecontents*}{DATA.csv}
x,y,z
0,0,0
0,0,1
1,1,0
1,1,1
2,2,0
2,4,1
3,3,0
3,9,1
4,4,0
4,16,1
5,5,0
5,25,1
6,6,0
6,36,1
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[/pgf/number format/.cd, use comma]
\addplot
table[x=x,y expr={ifthenelse(\thisrow{z}==0,\thisrow{y},nan)}, col sep=comma]
{DATA.csv};

\addplot
table[x=x,y expr={ifthenelse(\thisrow{z}==1,\thisrow{y},nan)},col sep=comma]
{DATA.csv};

\end{axis}
\end{tikzpicture}
\end{document}