# pgfplots Producing Incorrect Plot

Why is this not working properly:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin = 0,xmax= 1,ylabel = $C_A/C_A^*$,xlabel=$z$]
\foreach \x in {.01,.1,1,10}
\end{axis}
\end{tikzpicture}
\end{document}


Produces:

While it should produce:

I have tried to increase the samples in the addplot options to no avail.

• you are using x and \x in the same equation which confuses it. change all instances of \x with something like \myvar and it works – ArTourter Apr 14 '15 at 17:22
• @ArTourter Thanks, that works. But why so? – jak123 Apr 14 '15 at 17:27
• Just a suggestion - use parenthesis around every variable in an expression, as when the variable has a negative value, - sign may change the order of calculation. Example: \x^2 is not the same as (\x)^2 when x is negative. Correct me if I am wrong, but that was my impression from playing with pgfplot. See this: tex.stackexchange.com/a/125896/37570 – ajeh Apr 14 '15 at 18:31

pgfplots understands the macro and the character versions of the variables. From the manual

What really goes on is a loop which assigns the current sample coordinate to the macro \x.

pgfplots defines a math constant x which always has the same value as \x. In short: it is the same whether you write \x or just x inside of math expressions.

So that fixes the first problem but if you use this foreach loop for other variables in the options say to use as a color modifier etc. it still won't work. This is the common problem of having a macro within an \addplot command unexpanded such as this explanation : How to node foreach value in math equation?

Hence, it is a good practice to loop with pgfplots's own loop macro as below(I would also use sqrt(#1) instead):

\begin{tikzpicture}
\begin{axis}[xmin = 0,xmax= 1,ylabel = $C_A/C_A^*$,xlabel=$z$]
\pgfplotsinvokeforeach{.01,.1,1,10}{
`