3

I think this is a relatively simple question but I can't seem to find the answer anywhere.

Suppose I have the following set of parametric equations for a cylinder:

X(s,t) = cos(s), Y(s,t) = sin(s), Z(s,t) = t

that I can plot using the following snippet of code:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}

\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \addplot3[surf, 
              samples=25, 
              z buffer = sort,
              domain = 0:2*pi, 
              y domain = 0:1]({cos(deg(x))}, {sin(deg(x)}, {y});
\end{axis}
\end{tikzpicture}

\end{document}

that generates the following figure:

Cylinder

I would like to change the color shading from what is currently the z-value to some other parametric function, say w(s,t)=1+5*s^2+exp(t). I've tried using the declare function feature in pgfplots along with point meta but have had no luck. This seems like it should be something that's fairly straight-forward but it's proving to be quite difficult. I'm also fairly new to tikz/pgfplots which may explain it.

Thanks!

3

Welcome to TeX-SE! I may be missing something but I just spelled out what you seem to be trying and it works here.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}

\begin{document}
\begin{tikzpicture}[declare function={myshader(\x,\y)=cos(deg(2*\x))-0.6*sin(deg(\y));}]
  \begin{axis}
    \addplot3[surf, 
              samples=25, 
              z buffer = sort,
              domain = 0:2*pi, 
              y domain = 0:1,
              point meta={myshader(x,y)}]({cos(deg(x))}, {sin(deg(x)}, {y});
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you for the response! This actually isn't what I'm looking for, since the contour function here is using the physical (x,y)-coordinates and not the parametric ones. Perhaps I need to rephrase my initial posting to make this more clear. – JBenzaken May 28 '19 at 15:52
  • @JBenzaken One simple way to get the parameters is to invert the function, e.g. point meta={myshader(atan2(rawy,rawx),rawz)}]({cos(deg(x))}, {sin(deg(x)}, {y});. – user121799 May 28 '19 at 15:58
  • Is there any other way to do this without having to explicitly obtain the parameters through an inverse? The geometries I'm actually using are much more complex and this is not an option. – JBenzaken May 28 '19 at 16:00
  • @JBenzaken I do not know out of the top of my head, and it might be less trivial than you think it should be because of the way coordinates are parsed. (Note that some things not discussed in the manual is explained in detail in pgfplotscoordprocessing.code.tex.) – user121799 May 28 '19 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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