2

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...

\documentclass{article}
\usepackage{color} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\pgfplotsset{width=7cm,compat=1.3} 
\usepackage{tikz}

\begin{document}
\begin{figure}
   \centering
   \begin{tikzpicture}
      \begin{loglogaxis}
         \foreach \n in {1,...,20} {
            \addplot[
               color=blue,
            ] table {data/datafile_\n.dat};
         }
      \end{loglogaxis}
   \end{tikzpicture}
\end{figure}
\end{document}

I would like to have something like

color=blue!\n!white

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

\pgfplotsinvokeforeach{1,...,20}{

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

 \pgfmathtruncatemacro{\k}{#1*5}

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!

0

1 Answer 1

2

Do you mean this?

\documentclass[border=2mm]{standalone}
\usepackage{color}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.3,
    }
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \begin{axis}
        % use this \foreach variant to cycle through the number of items
        \pgfplotsforeachungrouped \n in {1,...,10} {
                % manipulate the counter and store it in `\i'
                \pgfmathtruncatemacro{\i}{10*\n}
            % then use this construct as written in the manual in section 8.1
            % on page 519 (v1.13)
            \edef\temp{\noexpand%
            \addplot[
                color=blue!\i,
            ] coordinates { (0,\n) (1,\n) };
            }\temp
        }
        % 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) };
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

1
  • 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 .