# parametric line renders a mesh: how functions are parsed

In the code below I define three functions u(x, y), v(x, y) and w(x, y) which just define a simple parameterization of the unit sphere

u(x, y) = sin(x) * cos(y)
v(x, y) = sin(x) * sin(y)
w(x, y) = cos(x)


I proceed to plot three different sets: 1: The whole surface of the sphere. 2 Points for which azimuthal angle is zero y = 0. 3 Same as 2, but explicitly changing the functions

\documentclass[tikz, border = 10pt]{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
domain            = 0 : 180,
y domain          = 0 : 360,
declare function  = {
u(\x, \y) = sin(\x) * cos(\y);
v(\x, \y) = sin(\x) * sin(\y);
w(\x, \y) = cos(\x);
}
]

% 1. sphere
\addplot3 [surf, opacity = 0.1] ({u(\x, \y)}, {v(\x, \y)}, {w(\x, \y)});

% 2. y = 0
\addplot3 [red, opacity = 1] ({u(\x, 0)}, {v(\x, 0)}, {w(\x, 0)});

% 3. same as before?
\addplot3 [blue, samples y = 0, opacity = 1] ({sin(\x) * cos(0)}, {sin(\x) * sin(0)}, {cos(\x)});

\end{axis}
\end{tikzpicture}
\end{document}


I was expecting to see a single red line (the second set), instead I see the whole mesh again. But If I explicitly set y = 0 in the function (blue line) it works. Why is that? I assume it has to do with the way functions are parsed, but that's about it ... no idea what's happening

• @marmot I forgot to mention, if I explicitly say samples y = 0 I get an error Package pgfplots Error: Sorry, you can't use 'y' in this context. PGFPlots ex pected to sample a line, not a mesh (...) – caverac Jan 8 at 13:27
• I am wondering if this should be mentioned to the pgfplots developer. After all the error message here is not necessarily illuminating. If it had Do you have excess spaces in you function declarations? in addition this may help to avoid some fair amount of headache. – marmot Jan 8 at 22:06
• @marmot Probably should, this was very frustrating – caverac Jan 8 at 22:17

OMG! Spaces! All you need to do is to remove the spaces before \y inside declare function. That is, u(\x, \y) = sin(\x) * cos(\y); has to become u(\x,\y) = sin(\x) * cos(\y); and so on. (@egreg, where are you. why didn't you see this? ;-)

\documentclass[tikz, border = 10pt]{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
domain            = 0 : 180,
y domain          = 0 : 360,
declare function  = {
u(\x,\y) = sin(\x) * cos(\y);
v(\x,\y) = sin(\x) * sin(\y);
w(\x,\y) = cos(\x);
}
]

% 1. sphere
\addplot3 [surf, opacity = 0.1] ({u(\x, \y)}, {v(\x, \y)}, {w(\x, \y)});

% 2. y = 0
\addplot3 [red, opacity = 1] ({u(\x, 0)}, {v(\x, 0)}, {w(\x, 0)});

% 3. same as before?
%\addplot3 [blue, samples y = 0, opacity = 1] ({sin(\x) * cos(0)}, {sin(\x) * sin(0)}, {cos(\x)});

\end{axis}
\end{tikzpicture}
\end{document}


Yes, I know that the plot is closed, but this is not the point here. You could do

 \addplot3 [mesh,color=red,samples y=0, opacity = 1] ({u(\x, 0)}, {v(\x, 0)}, {w(\x, 0)});


• @caverac I didn't know this. Ironically, you can have spaces like crazy in \addplot3 [surf, opacity = 0.1] ({u(\x, \y)}, {v(\x, \y)}, {w(\x, \y)});. – marmot Jan 8 at 21:49