[Tex/LaTex] Tikz: \foreach and axis environment incompatibility

foreachpgfplotstikz-pgf

I'm trying to plot a figure with TikZ. I have a few .txt files with the coordinates of the curves that I want to plot, in a list such as

file2.txt, 
file4.txt,
...
file30.txt

and I want to add them with \plot file in a axis environment. Also, I'd like to put them with different colors, creating a shading effects, let's say from blue to red.

In short, I want to create a sort of contour plot, but since I don't have the 3D function I have to import the coordinates.

I wrote this code, where I use two variables in \foreach, one for the file name, \x, and one for the shading of the color, \y, (see lines 'Original Idea'). It doesn't work because of some kind of trouble between \foreach and the axis environment.

I thought about other options, such as to explicate a relation between \x and \y (see code lines), use \pgfplotsinvokeforeach or an other trick with \edef but they doesn't work for different reasons:

  • if I explicit the relation it seem like it doesn't want to calculate 3*\x (undefined control sequence)

  • with \pgfplotsinvokeforeach it says 'illegal unit of measure'

  • same for \edef

I read some topics on the use of \pgfplotsinvokeforeach and the trick with \edef but probably I haven't understood correctly how to use them in my code.

Can you give me a solution? Better it would be if you give me the corrections for all the 4 possible cases I proposed 🙂

Here's the code

\documentclass{article}
\usepackage{pgf,pgfsys,pgffor}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}

\begin{figure}
    \begin{tikzpicture} 
    \newcommand{\filename}{./figure/regionDep0/orthogonal}
    \begin{axis}[
        xmin=0, xmax=1, ymin=0, ymax=1, width=8cm, height=8cm]
        % Original idea: not working
        \foreach \x/\y in {2/0,4/7,...,30/98}
        {
            \plot[color=blue!\y!red] file {\filename \x.txt};
        }
%   
%       % substituting \y as function of \x: not working
%       \foreach \x in {2,4,...,30}
%       {
%           \plot[color=blue!3*\x!red] file {\filename \x.txt};
%       }
%       
%       % Trying with \pgfplotsinvokeforeach: not working
%       \pgfplotsinvokeforeach{2,4,...,30}
%       {
%       \plot [color=blue!3*#1!red] file {\filename #1.txt};
%       }
%
%       %   Trying with \edef\temp{...}: not working
%       \foreach \x/\y in {2/0,4/7,...,30/98}
%       {
%           \edef\temp{\noexpand\plot [color=blue!\y!red] file {\filename \x.txt};}
%           \temp
%       }       
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

Best Answer

If you're using an axis environment, which is provided by the PGFPlots package, you should use \addplot instead of \plot.

To use a different colour for each plot, one option might be using the mesh plot type together with point meta=explicit and meta expr=\i. This allows the use of colormaps instead of being limited to colorA!<value>!colorB expressions:

\documentclass{article}
\usepackage{pgfplots}  % Loads all required packages automatically
\usepackage{filecontents}
\begin{filecontents*}{file1.txt}
0 0
1 1
2 2
3 1
4 0
\end{filecontents*}
\begin{filecontents*}{file2.txt}
0 1
1 3
2 5
3 3
4 1
\end{filecontents*}
\begin{filecontents*}{file3.txt}
0 2
1 5
2 7
3 5
4 2
\end{filecontents*}

\begin{document}

\begin{figure}
    \begin{tikzpicture} 
    \newcommand{\filename}{file}
    \begin{axis}[
            colormap/bluered
        ]
        \foreach \i in {1,2,3}
        {
            \addplot +[thick, no markers, mesh, point meta=explicit] table [meta expr=\i] {\filename \i.txt};
        }   
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

Alternatively (and probably better), you can use the contour prepared plot style if you put all contours into a single file. That way, you can even get labels on the contours (the labels can be adjusted or switched off, of course):

\documentclass{article}
\usepackage{pgfplots}  % Loads all required packages automatically
\pgfkeys{/pgf/number format/relative round mode=fixed}
\usepackage{filecontents}
\begin{filecontents*}{contours.txt}
0 0 0
1 1 0
2 2 0
3 1 0
4 0 0

0 1 1
1 3 1
2 5 1
3 3 1
4 1 1

0 2 2
1 5 2
2 7 2
3 5 2
4 2 2
\end{filecontents*}

\begin{document}

\begin{figure}
    \begin{tikzpicture} 
    \newcommand{\filename}{file}
    \begin{axis}
    \addplot [contour prepared] table {contours.txt};
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}
Related Question