Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

\pgfplotstableread{data.dat}\datafile

\begin{tikzpicture}
\begin{axis}
\addplot[x filter/.code={
\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};
\addplot[x filter/.code={
\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.

share|improve this question
    
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 '13 at 8:45
add comment

1 Answer

up vote 2 down vote accepted

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}


\pgfplotstableread{data.dat}\datafile

\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}
\addplot[mystyle={\datafile}{1},mark=o] table[x=x,y=y, meta=color] {\datafile};
\addplot[mystyle={\datafile}{0},mark=square] table[x=x,y=y, meta=color] {\datafile};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

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.

share|improve this answer
    
Unfortunately, this approach seems to be leading me to 'TeX capacity exceeded' problems (for the actual document). I wasn't seeing it when the filtering was in each addplot line. This is happening even with tikzexternalize. –  ackey Mar 2 '13 at 9:26
1  
@ackey It's strange indeed. Especially when everything is working with the explicit options. Maybe my cook-up isn't that innocent looking behind the scenes. Is it working with mildly large files or just doesn't work even with 50 items? –  percusse Mar 2 '13 at 10:08
    
It is working now. I introduced some bugs when I set up the styles, but finally have them worked out now. Your answer is good, and I'll select it if no one has a method for the name-aware solution. Thanks! –  ackey Mar 2 '13 at 10:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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