I'm trying to automatically separate graphs in pgfplots. What I mean by this is something like this: IfI have s file like this as input:

x z  y
1 4  1
2 5  1
3 0  2
4 10 2

Now I'd like to have the entries with the 1 in one plot and 2 in another plot. Since I want to have this automatically, I'd like to not to this with multiple manual \addplots.

The solution I came up with this up to now, is something like this:

\foreach \var in {1,2,..,50}{
  \addplot[y filter/.expression={\thisrow{z}==\var ? y : nan}, unbounded coords=jump] ...

But this has to cycle about every possible value of z which takes very long and is completely unnecessary. Is there a way to cycle only about the present values or to take the list to cylce about from another file (this would be a fix for me too)?

EDIT: I solve this now by writing the numbers I want to go through into another file, with \def\iterate{<numbers>}, use \input{...} in the file that generates the graph and iterate then over the contents of the defined macro. (Not a nice solution, but it works)

  • 1
    Instead of writing the answer into question, please move it to an answer. And optimaly you would elaborate a bit more and/or provide a full working MWE, so others have it easy to follow/adapt your solution. Many thanks in advance. Commented Jul 7, 2020 at 4:04
  • Ok, I just see me "solution" only as a quick and dirty fix, so I didn't wanted to make an answer out of this, but if it is wished, I'll do it in the next days ;)
    – atticus
    Commented Jul 10, 2020 at 11:13
  • 1
    When you are not satisfied with your solution you could just state it and don't accept it. If someone else encounters the same "problem" then there at least is this "dirty fix" which might be better than nothing at all ;) Commented Jul 10, 2020 at 13:10

1 Answer 1


Well my kind of solution for this was to create a file which contains the declaration of a TeX-macro so that I can include that file into the main latex File and iterate over the declared macro.


File to generate the macro



% fileHeader
% pixelCount scaling time

                        \foreach \var in  \myMacro{
                                \addplot+[red, mark size=2pt, y filter/.expression={\thisrow{rowToFilterFor}==\var ? y : nan}] table[x=column1, y=column2, col sep=space] {inputDataFile.dat}

I found this much easier than searching and editing the line where the list to iterate over is declared from the outside (in my case a bash-script) and writing that string to a file isn't that difficult.

PS: And yes this isn't a nice solution, but it works for now, more elegant solutions are always welcome ;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .