I am looping over several files and plotting the content on an axis in pgfplots. I would like to plot each entry as a different color (a shade of blue, for example). While the plotting works fine, I'm having a lot of trouble being able to change the color in each 'for' loop. There are similar questions here, but I can't get any to work quite right.

here is a simple example...


         \foreach \n in {1,...,20} {
            ] table {data/datafile_\n.dat};

I would like to have something like


but that doesn't seem to have any effect on the plot. More specifically, I would like to perform some operation on the counter \n to have more control of the color. Fumbling around at similar questions and the pgfplots manual, I have tried replacing the \foreach loop with


This lets me do something like color=blue!#1!white, but if I want to operate on the counter itself with something like


and then use color=blue!\k!white, I don't get the desired result - the color stays constant.

Extremely new to pgfplots and tikz, so any help would be much appreciated!


1 Answer 1


Do you mean this?

        % use this \foreach variant to cycle through the number of items
        \pgfplotsforeachungrouped \n in {1,...,10} {
                % manipulate the counter and store it in `\i'
            % then use this construct as written in the manual in section 8.1
            % on page 519 (v1.13)
            ] coordinates { (0,\n) (1,\n) };
        % test, if it worked by adding some dummy points with the expected colors
        \addplot [blue!10,only marks,mark=*]  coordinates { (0.5,1) };
        \addplot [blue!100,only marks,mark=*] coordinates { (0.5,10) };

image showing the result of above code

  • Exactly what I was looking for. Thanks for the detailed response!
    – Brian
    Commented Apr 19, 2016 at 17:54

You must log in to answer this question.

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