2

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}
\pgfplotsset{compat=newest}

\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}

enter image description here

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

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

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}
\pgfplotsset{compat=newest}

\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}

enter image description here

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)});

instead.

enter image description here

  • lol ... another cruel reminder this is a text processor :) – caverac Jan 8 at 21:48
  • @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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.