# Plotting curves within a foreach loop and attributing colors from a colormap

I'm trying to plot curves inside a \foreach loop and at the same time define their color through a colormap. The color is given accordingly to a parameter of the curve:(\T/\Tmax*1000), where \T is the value of an element of the array \Tarray and \Tmax is the maximum value of these elements. To do this I used the command \pgfplotscolormapdefinemappedcolor. The full code is the following:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[scale=1, every node/.style={scale=1}]

%--->Parameters
\def\Xmin{0}
\def\Xmax{10}
\def\Tarray{1,2,...,10}
\def\Tmax{10}

%--->Plot

\begin{axis}
[
legend pos = north east,
xlabel={$x$},
ylabel={$f$},
ymajorgrids=true,
grid=both,
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
colormap/jet,
colorT/.style={/utils/exec={\pgfplotscolormapdefinemappedcolor{#1}},color=mapped color}] %Define the variable colorT that corresponds to the color number in the colormap (from 0 to 1000)
]

\foreach \T in \Tarray
{
[
domain=\Xmin:\Xmax,
samples=100,
colorT=(\T/\Tmax*1000)
]
{x/\T*e^(-x/\T)};
\addlegendentryexpanded{$T=\T$}
}
\end{axis}

\end{tikzpicture}
\end{document}


When I compile the code in Texmaker the following error appears:

! Package PGF Math Error: Could not parse input '(1/10*1000)' as a floating point number, sorry

It seems that (\T/\Tmax*1000) in colorT is not being evaluated. If I substitute (\T/\Tmax*1000) in the code by a numerical value, for example 1, no error appears, and I get the plot that I desire:

How can I make the code to evaluate (\T/\Tmax*1000)?

There are two major changes:

1. The parsing error comes from the number format used by fpu. To solve the problem, I use \PgfmathparseFPU (which also has been used e.g. here.) It locally changes the number format to something that TikZ can digest.
2. You were also facing an expansion issue. I am using the \edef\temp trick that is described in the pgfplots manual in section 8.1 Utility Commands. If you find this too hacky, you could use \pgfplotsinvokeforeach instead.

Apart from that I added \pgfplotsset{compat=1.16} and slightly changed the parsing order to address the issue you raised in the comments.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\def\PgfmathparseFPU#1{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathparse{#1}%
\pgfmathsmuggle\pgfmathresult\endgroup}%

\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[scale=1, every node/.style={scale=1}]

%--->Parameters
\def\Xmin{0}
\def\Xmax{10}
\def\Tarray{1,2,...,10}
\def\Tmax{10}

%--->Plot

\begin{axis}
[
legend pos = north east,
xlabel={$x$},
ylabel={$f$},
ymajorgrids=true,
grid=both,
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
colormap/jet,
colorT/.style={/utils/exec={\PgfmathparseFPU{#1}%
\pgfplotscolormapdefinemappedcolor{\pgfmathresult}},color=mapped color}] %Define the variable colorT that corresponds to the color number in the colormap (from 0 to 1000)
]

\foreach \T in \Tarray
{
[
domain=\Xmin:\Xmax,
samples=100,
colorT={\T*(1000/\Tmax)}
]
{x/\T*e^(-x/\T)};
\noexpand\addlegendentryexpanded{$T=\T$}}
\temp
}
\end{axis}

\end{tikzpicture}
\end{document}


• It indeed worked. Thank you! I think I understood the changes. They involve commands that I wasn't currently aware of. But explanations are always welcome! – Élio Pereira Sep 22 at 14:54
• One more question: an error "Dimension too large" appears when \T or \Tmax in (\T/\Tmax*1000) is bigger than 16383.99998 (I read that this is TeX’s largest dimension) although the result of the operation is lower than this value. How can I make the compiler to accept these huge values? – Élio Pereira Sep 22 at 16:32
• @ÉlioPereira I added an explanation and made some other minor changes, hoping to solve the dimension too large error. Note, however, that I never got these errors. If they persist, please tell me which inputs you are using. – Schrödinger's cat Sep 22 at 20:01
• The error still happens. Try for example to change the definition of the \Tarray to \def\Tarray{10000,20000,...,100000}. You will get an error of the type "Dimensions too large". Anyway your answer was pretty useful and I may work with different units for \Tarray so that lower values are attained. – Élio Pereira Sep 22 at 21:26
• @ÉlioPereira This is a limitation of \foreach. I do not see any easy way to rectify this other than what you are suggesting. – Schrödinger's cat Sep 22 at 21:34