5

I 've used Tikz to generate this 2D plot. I want to get its 3D version by rotating it throw the axis z. Is there any Tikz special command that can do that ? Thank you

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=6]
\draw[-] (0,2,0)--(-0.5,1,0).. controls (-1,0,0) and (1,0,0).. 
   (0.5,1,0)--(0,2,0);
 \draw[dashed](0,-0.1,0)--(0,2.1,0);
 \node [above] at (0,2.1,0) {$\boldsymbol{Z}$};
\end{tikzpicture}
\end{center}    
 \end{document}
  • 1
    Asymptote is a more appropriate package for 3d modelling, at the moment. – Benjamin McKay Oct 14 '19 at 17:17
5

Yes, there is. tikz-3dplot with the 3d library that it auto-loads. The rotation angle is the second argument of \tdplotsetmaincoords{90}{70} in the following MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{amsmath}
\begin{document}
\begin{center}
\tdplotsetmaincoords{90}{70} 
\begin{tikzpicture}[tdplot_main_coords,scale=6,canvas is xz plane at y=0]
\draw[-] (0,2)--(-0.5,1).. controls (-1,0) and (1,0).. 
   (0.5,1)--(0,2);
 \draw[dashed](0,-0.1)--(0,2.1);
 \node [above] at (0,2.1) {$\boldsymbol{Z}$};
\end{tikzpicture}
\end{center}    
\end{document}

enter image description here

An animation for illustration what the rotation angle means.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\usepackage{amsmath}
\begin{document}
\foreach \X in {5,15,...,355}
{\tdplotsetmaincoords{90}{\X} 
\begin{tikzpicture}[tdplot_main_coords,scale=6]
\path[use as bounding box,tdplot_screen_coords] (-1,-0.5) rectangle (1,2.5);
\begin{scope}[canvas is xz plane at y=0]
\draw[-] (0,2)--(-0.5,1).. controls (-1,0) and (1,0).. 
   (0.5,1)--(0,2);
 \draw[dashed](0,-0.1)--(0,2.1);
 \node [above] at (0,2.1) {$\boldsymbol{Z}$};
\end{scope} 
\end{tikzpicture}}
\end{document}

enter image description here

This can be used to fill a surface.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\usepackage{amsmath}
\begin{document}

\begin{tikzpicture}[scale=6]
\tdplotsetmaincoords{90}{0} 
 \path[use as bounding box,tdplot_screen_coords] (-1,-0.5) rectangle (1,2.5);
 \foreach \X [count=\Y] in {0,1,...,89}
 {\tdplotsetrotatedcoords{00}{0}{\X}
 \pgfmathtruncatemacro{\ccol}{100-0.7*\Y} 
 \begin{scope}[tdplot_rotated_coords,canvas is xz plane at y=0]
  \path[fill=blue!\ccol] (0,2)--(-0.5,1).. controls (-1,0) and (1,0).. 
    (0.5,1)--(0,2);
 \end{scope} }
 \draw[dashed](0,-0.1)--(0,2.1);
 \node [above] at (0,2.1) {$\boldsymbol{Z}$};
\end{tikzpicture}
\end{document}

enter image description here

ADDENDUM: As for the clarified question: AFAIK there is not a simple switch that provides you with the surface. However, if you have a function that approximates the Bezier curve, you can use pgfplots to draw the rotational surface.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\usepackage{amsmath}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[unit vector ratio=1 1 1,scale=6,hide axis,colormap/viridis]
\addplot3[surf,shader=interp,z buffer=sort,domain y=0:180,domain=0:360]
({2*sin(x)*sin(y)/3},{2*cos(x)*sin(y)/3},{0.5*(1+ifthenelse(abs(y)<90,(90-abs(y))*6/180,0.7*cos(y)))});  
\end{axis}
\end{tikzpicture}
\end{center}    
\end{document}

enter image description here

Yet I agree with Benjamin McKay that asymptote is a more appropriate tool for that.

| improve this answer | |
  • this is not a 3D plot ! my question is about getting a 3d surface from may 2d one – rihani Oct 14 '19 at 17:11
  • 2
    @rihani Sorry, misunderstanding, but your question does not mention "surface". – user194703 Oct 14 '19 at 17:28
  • Nice answer marmot. Erm, old marmot. Erm, Schrödinger's cat! – manooooh Oct 15 '19 at 4:45

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.