22

enter image description herehow to draw using surface of revolution tikz or pgfplots?

\documentclass{article}
\usepackage{tikz,pgfplots}

\begin{document}
\begin{tikzpicture}

\end{tikzpicture}
\end{document}

I am not able to reproduce a surface like this. Can anyone help me? enter image description here

Regis Santos
  • 14,463
  • 7
    You should open a new question for this, instead of changing your existing one and unaccepting the answer. – Jake Nov 11 '11 at 00:29
  • 5
    Adding to the comment by @Jake, in the new question you should at least make an attempt in adapting the solution below to this new problem as they are clearly related. – Peter Grill Nov 11 '11 at 00:34

1 Answers1

32

In the plots below I've given two demonstrations: one is a surface rotated around the y-axis, and one is rotated around the x-axis

The main trick is to parametrize the surface appropriately using the sine and cosine functions.

When you rotate the function f(t) around the y-axis, then you let

x(t,s) = t*cos(s)
y(t,s) = t*sin(s)
z(t,s) = f(t)

If you want to rotate the function f(t) around the x-axis, then you let

x(t,s) = t
y(t,s) = f(t)*cos(s)
z(t,s) = f(t)*sin(s)

Typically s will be on the interval [0,2\pi], and you can choose your interval for t as you like.

screenshot

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

% rotated around the y-axis
\begin{tikzpicture}
 \begin{axis}[view={60}{30}]
  \addplot3[surf,shader=flat,
  samples=20,
  domain=1:2,y domain=0:2*pi,
  z buffer=sort]
  ({x * cos(deg(y))}, {x * sin(deg(y))}, {1/x});
 \end{axis}
\end{tikzpicture}

% rotated around the x-axis
\begin{tikzpicture}
 \begin{axis}[view={60}{30}]
  \addplot3[surf,shader=flat,
  samples=20,
  domain=1:2,y domain=0:2*pi,
  z buffer=sort]
  (x,{(1/x) * cos(deg(y))}, {(1/x) * sin(deg(y))});
 \end{axis}
\end{tikzpicture}

\end{document}

Following the question edit

enter image description here

\begin{tikzpicture}
 \begin{axis}[view={60}{30}]
  \addplot3[surf,shader=flat,
  samples=20,
  domain=0:2*pi,y domain=0:2*pi,
  z buffer=sort]
  ({x * cos(deg(y))}, {x * sin(deg(y))}, {cos(deg(x))});
 \end{axis}
\end{tikzpicture}
cmhughes
  • 100,947
  • @RegisdaSilva: You're welcome; you can tweak some further options such as label, perhaps even transparency, but this code gives the main idea :) – cmhughes Nov 10 '11 at 04:07
  • New help, please. – Regis Santos Nov 11 '11 at 00:22
  • 11
    @RegisdaSilva I'm very happy to help, but it seems a little unfair to un-accept my answer now that you've changed the question. I did answer the original question. – cmhughes Nov 11 '11 at 00:27
  • @RegisdaSilva See the update – cmhughes Nov 11 '11 at 01:09
  • Hmm ... that one looks much nicer. Let's see if I can recreate it with TikZ and your answer. – Martin Thoma Nov 19 '13 at 12:40
  • 1
    Update: recent versions of pgfplots allow to use builtin coordinate systems. A good extension to this answer could be to rely on data cs=polarrad and specify the input coordinates in the coordinate system <angle>,<radius>,<Z> . See also http://tex.stackexchange.com/questions/173602/pgfplots-3d-creating-a-filled-solid-of-revolution/174077#174077 for another application – Christian Feuersänger Apr 28 '14 at 21:29