# PGFPLOTS coordinate filtering as a style?

I am using pgfplots to make plots for my thesis, and I have many scatter plots that I want to have consistent formatting. I am controlling the shape and color of the scatter plot marks with two columns (from external files loaded through \pgfplotstableread) that have consistent names across the many input files.

So far, my solution is to use one as to define scatter/classes and to use the other to filter. This has worked fine, but I would like to have the filter parameters specified as a style, rather than needing to repeat the filter code on every \addplot command.

What is working:

\documentclass{article}

\usepackage{pgfplots,pgfplotstable,tikz}
\usepackage{filecontents}

\begin{filecontents}{data.dat}
x y color shape
1 3 a 0
2 6 b 0
3 5 a 1
4 4 b 1
5 9 a 0
6 1 b 1
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\begin{axis}
\pgfplotstablegetelem{\coordindex}{shape}\of{\datafile}
\ifnum\pgfplotsretval=0
\else
\def\pgfmathresult{}
\fi},only marks, scatter, scatter src=explicit symbolic,  scatter/classes={
a={blue},%
b={red}%
},mark=o] table[x=x,y=y, meta=color] {\datafile};
\pgfplotstablegetelem{\coordindex}{shape}\of{\datafile}
\ifnum\pgfplotsretval=1
\else
\def\pgfmathresult{}
\fi},only marks, scatter, scatter src=explicit symbolic, scatter/classes={
a={blue},%
b={red}%
},mark=square] table[x=x,y=y, meta=color] {\datafile};
\end{axis}
\end{tikzpicture}

\end{document}


I can't just plug the x filter code as it is into a style, since it uses the name of the table (which changes). I've tried the discard if not style, but it doesn't work with \pgfplotstableread

Can I put the filtering code in a style and modify it so it automatically gets the correct table (being used for everything else in the plot)?

I'm aware that I could approach the entire problem differently by using \pgfplotstableset to create a new column in the table that has (somehow) concatenated the color and shape, and then have (2x2 in the example) 4 different scatter classes.

-
 I hadn't realized that styles took an argument like that. That isn't as great as being able to automatically get the table name, but it certainly helps. – ackey Mar 2 at 8:45

Until a table-name aware solution arrives you can create your own style to reduce the repetition.

\documentclass{article}

\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.7}
\usepackage{filecontents}

\begin{filecontents}{data.dat}
x y color shape
1 3 a 0
2 6 b 0
3 5 a 1
4 4 b 1
5 9 a 0
6 1 b 1
\end{filecontents}

\pgfplotsset{
mystyle/.style 2 args={
x filter/.code={%
\pgfplotstablegetelem{\coordindex}{shape}\of#1
\ifnum\pgfplotsretval=#2\relax
\else
\def\pgfmathresult{}%
\fi
},
only marks, scatter, scatter src=explicit symbolic,
scatter/classes={
a={blue},%
b={red}%
}
}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}

As an ugly hack, you can then define a new function that includes the whole \addplot.... where you can change the table name in both places.