# Draw in cylindrical and spherical coordinates

I can try to draw this in TikZ:

I managed to draw the coordinate axis. The first image is in cylindrical coordinates and the second in spherical coordinates. I don't know draw in spherical coordinate system, the arrow labels, curved lines, and many other things.

I have started to read the manual of Till Tantau, but for now I'm a newbie with TikZ and I don't understand many things of this manual.

• – Alain Matthes Feb 9 '14 at 10:56
• @AlainMatthes - Thanks, does tikz-3dplot package is required? – Kiyoshi Feb 9 '14 at 13:11
• I can't draw the curved lines between two points... – Kiyoshi Feb 9 '14 at 14:10

## 4 Answers

I don't understand the 3D behavior of tikz very well, but here's a way to do one of your pictures in Asymptote using a bunch of lines, arcs, and labels.

A few of the built-in Asymptote commands I used:

• X is the unit vector (1,0,0) similarly for Y and Z
• expi(theta,phi) returns the unit vector in the theta,phi direction

Updated to incorporate a few of Charles Staats' suggestions

• Changed the dashed lines to solid lines
• Reduced weight on the labeling lines and arcs and increased weight on axes
• added a light, nearly transparent spherical surface on which the volume element lives . . . I think this helps with perspective

\documentclass{article}
\usepackage{asymptote}

\begin{document}

\begin{asy}[width=0.5\textwidth]
settings.render=6;
settings.prc=false;
import three;
import graph3;
import grid3;
currentprojection=orthographic(1,-0.175,0.33,up=Z);

//Draw Axes
pen thickblack = black+0.75;
real axislength = 1.33;
draw(L=Label("$x$", position=Relative(1.1), align=SW), O--axislength*X,thickblack, Arrow3);
draw(L=Label("$y$", position=Relative(1.1), align=N), O--axislength*Y,thickblack, Arrow3);
draw(L=Label("$z$", position=Relative(1.1), align=N), O--axislength*Z,thickblack, Arrow3);

//Set parameters of start corner of polar volume element
real r = 1;
real q=0.3pi; //theta
real f=0.35pi; //phi

real dq=0.15; //dtheta
real df=0.3; //dphi
real dr=0.1;

// Arq is A + dr*rhat + dq*qhat, etc
triple A = r*expi(q,f);
triple Ar = (r+dr)*expi(q,f);
triple Aq = r*expi(q+dq,f);
triple Arq = (r+dr)*expi(q+dq,f);
triple Af = r*expi(q,f+df);
triple Arf = (r+dr)*expi(q,f+df);
triple Aqf = r*expi(q+dq,f+df);
triple Arqf = (r+dr)*expi(q+dq,f+df);

pen thingray = gray+0.33;

draw(A--Ar);
draw(Aq--Arq);
draw(Af--Arf);
draw(Aqf--Arqf);
draw( arc(O,A,Aq) ,thickblack );
draw( arc(O,Af,Aqf),thickblack );
draw( arc(O,Ar,Arq) );
draw( arc(O,Arf,Arqf) );
draw( arc(O,Ar,Arq) );
draw( arc(O,A,Af),thickblack );
draw( arc(O,Aq,Aqf),thickblack );
draw( arc(O,Ar,Arf) );
draw( arc(O,Arq,Arqf) );

pen thinblack = black+0.25;

//phi arcs
draw(O--expi(pi/2,f),thinblack);
draw("$\varphi$", arc(O,0.5*X,0.5*expi(pi/2,f)),thinblack,Arrow3);
draw(O--expi(pi/2,f+df),thinblack);
draw( "$d\varphi$", arc(O,expi(pi/2,f),expi(pi/2,f+df) ),thinblack );
draw( A.z*Z -- A,thinblack);
draw(L=Label("$r\sin{\theta}$",position=Relative(0.5),align=N), A.z*Z -- Af,thinblack);

//cotheta arcs
draw( arc(O,Aq,expi(pi/2,f)),thinblack );
draw( arc(O,Aqf,expi(pi/2,f+df) ),thinblack);

//theta arcs
draw(O--A,thinblack);
draw(O--Aq,thinblack);
draw("$\theta$", arc(O,0.25*length(A)*Z,0.25*A),thinblack,Arrow3);
draw(L=Label("$d\theta$",position=Relative(0.5),align=NE) ,arc(O,0.66*A,0.66*Aq),thinblack );

// inner surface
triple rin(pair t) {  return r*expi(t.x,t.y);}
surface inner=surface(rin,(q,f),(q+dq,f+df),16,16);
draw(inner,emissive(gray+opacity(0.33)));
//part of a nearly transparent sphere to help see perspective
surface sphere=surface(rin,(0,0),(pi/2,pi/2),16,16);
draw(sphere,emissive(gray+opacity(0.125)));

// dr and rdtheta labels
draw(L=Label("$dr$",position=Relative(1.1)), Af + 0.5*(Arf-Af)--Af + 0.5*(Arf-Af)+0.25*Z,dotted);
triple U=expi(q+0.5*dq,f);
draw(L=Label("$rd\theta$",position=Relative(1.1)), r*U ---r*(1.33*U.x,1.33*U.y,U.z),dotted );

\end{asy}

\end{document}


Update #2

Charles Staats pointed out good parameters for an oblique projection, which better matches the original picture. Using currentprojection=obliqueX, width=\textwidth, and editing the labels a bit to better suit this projection:

Orthographic code:

\documentclass{article}
\usepackage{asymptote}

\begin{document}

\begin{asy}[width=\textwidth]
settings.render=6;
settings.prc=false;
import three;
import graph3;
import grid3;
currentprojection=obliqueX;

//Draw Axes
pen thickblack = black+0.75;
real axislength = 1.0;
draw(L=Label("$x$", position=Relative(1.1), align=SW), O--axislength*X,thickblack, Arrow3);
draw(L=Label("$y$", position=Relative(1.1), align=E), O--axislength*Y,thickblack, Arrow3);
draw(L=Label("$z$", position=Relative(1.1), align=N), O--axislength*Z,thickblack, Arrow3);

//Set parameters of start corner of polar volume element
real r = 1;
real q=0.25pi; //theta
real f=0.3pi; //phi

real dq=0.15; //dtheta
real df=0.15; //dphi
real dr=0.15;

triple A = r*expi(q,f);
triple Ar = (r+dr)*expi(q,f);
triple Aq = r*expi(q+dq,f);
triple Arq = (r+dr)*expi(q+dq,f);
triple Af = r*expi(q,f+df);
triple Arf = (r+dr)*expi(q,f+df);
triple Aqf = r*expi(q+dq,f+df);
triple Arqf = (r+dr)*expi(q+dq,f+df);

pen thingray = gray+0.33;

draw(A--Ar);
draw(Aq--Arq);
draw(Af--Arf);
draw(Aqf--Arqf);
draw( arc(O,A,Aq) ,thickblack );
draw( arc(O,Af,Aqf),thickblack );
draw( arc(O,Ar,Arq) );
draw( arc(O,Arf,Arqf) );
draw( arc(O,Ar,Arq) );
draw( arc(O,A,Af),thickblack );
draw( arc(O,Aq,Aqf),thickblack );
draw( arc(O,Ar,Arf) );
draw( arc(O,Arq,Arqf) );

pen thinblack = black+0.25;

//phi arcs
draw(O--expi(pi/2,f),thinblack);
draw("$\varphi$", arc(O,0.5*X,0.5*expi(pi/2,f)),thinblack,Arrow3);
draw(O--expi(pi/2,f+df),thinblack);
draw( "$d\varphi$", arc(O,expi(pi/2,f),expi(pi/2,f+df) ),thinblack );
draw( A.z*Z -- A,thinblack);
draw(L=Label("$r\sin{\theta}$",position=Relative(0.5),align=N), A.z*Z -- Af,thinblack);

//cotheta arcs
draw( arc(O,Aq,expi(pi/2,f)),thinblack );
draw( arc(O,Aqf,expi(pi/2,f+df) ),thinblack);

//theta arcs
draw(O--A,thinblack);
draw(O--Aq,thinblack);
draw("$\theta$", arc(O,0.25*length(A)*Z,0.25*A),thinblack,Arrow3);
draw(L=Label("$d\theta$",position=Relative(0.5),align=NE) ,arc(O,0.66*A,0.66*Aq),thinblack );

// inner surface
triple rin(pair t) {  return r*expi(t.x,t.y);}
surface inner=surface(rin,(q,f),(q+dq,f+df),16,16);
draw(inner,emissive(gray+opacity(0.33)));
//part of a nearly transparent sphere to help see perspective
surface sphere=surface(rin,(0,0),(pi/2,pi/2),16,16);
draw(sphere,emissive(gray+opacity(0.125)));

// dr and rdtheta labels
triple V= Af + 0.5*(Arf-Af);
draw(L=Label("$dr$",position=Relative(1.1)), V--(1.5*V.x,1.5*V.y,V.z),dotted);
triple U=expi(q+0.5*dq,f);
draw(L=Label("$rd\theta$",position=Relative(1.1)), r*U ---r*(1.66*U.x,1.66*U.y,U.z),dotted );

\end{asy}

\end{document}

• This is beautiful! Nevertheless, I can't resist a couple of points. The dashed line below dtheta should be solid, and there should be another line (solid, from this point of view) to the fourth corner of the surface. (The original diagram gets away with omitting this because that line would be blocked from view; but from your point of view, it's not.) – Charles Staats Feb 15 '14 at 0:24
• One of the great things about this answer is that it's easy to experiment with changing the parameters and the point of view. The one exception is the inner surface, which will work better from many points of view if you replace gray+opacity(0.25) by emissive(gray+opacity(0.25)). – Charles Staats Feb 15 '14 at 0:28
• One final point, for those interesting in playing around with this excellent diagram: the projection used in the original diagram is called obliqueX in Asymptote. – Charles Staats Feb 15 '14 at 0:34
• @CharlesStaats Thanks for the suggestions, I've made a few changes to incorporate them. (The dashed lines were a bad idea.) I'm not sure what you mean by the "fourth corner" of the surface, though. Also, I know that I have strayed a bit from the OP's diagram, but I added a light sphere that I think really helps with perspective. – Chris Chudzicki Feb 15 '14 at 1:01
• @CharlesStaats Thanks for pointing out obliqueX! Unfortunately [and it may just be the (theta,phi) values I chose] I find it much harder to see the dr length when using this perspective. – Chris Chudzicki Feb 15 '14 at 1:03

I have prepared a lecture notes on electromagnetic field theory for my students. I have drawn surfaces used in cylindrical coordinate system. For this I have used tikz-3dplot package. Here is the code and output. I hope this is a starting point for you.

\documentclass{article}
\usepackage{tikz,tikz-3dplot}
\begin{document}

\begin{figure}
\centering
\tdplotsetmaincoords{70}{120}
\begin{tikzpicture}[tdplot_main_coords][scale=0.75]
\tikzstyle{every node}=[font=\small]
\draw[thick,-latex] (0,0,0) -- (6,0,0) node[anchor=north east]{$x$};
\draw[thick,-latex] (0,0,0) -- (0,6,0) node[anchor=north west]{$y$};
\draw[thick,-latex] (0,0,0) -- (0,0,6) node[anchor=south]{$z$};
\draw [thick](0,0,0) circle (3);
\draw [thick](0,0,4) circle (3);
\draw [thick](1.9,-2.35,0) -- (1.9,-2.35,4) node[midway, left]{$r=r_1$ surface};
\draw [thick](-1.9,2.35,0) -- (-1.9,2.35,4);
\filldraw[fill=orange, nearly transparent] (-4,-4,4) -- (4,-4,4) --  (4,5,4) -- (-4,5,4) -- (-4,-4,4);
\filldraw[fill=blue, nearly transparent] (0,0,4) -- (5.2,6,4) --  (5.2,6,0) -- (0,0,0) -- (0,0,4);
\filldraw [color=blue](2,2.25,4) circle (0.075cm) ;
\draw (-4,5,4) node[anchor=south]{$z=z_1$ plane};
\draw (5.2,6,0) node[anchor=south west]{$\phi=\phi_1$ plane};
\node at (1.8,1,4)  { $P_1(r_1,\phi_1,z_1)$};
\draw[ultra thick,-latex](2,2.25,4) -- (3,3.45,4) node[anchor=north] {$\mathbf{a}_r$};
\draw[ultra thick,-latex](2,2.25,4) -- (1,2.5,4) node[anchor=north west] {$\mathbf{a}_\phi$};
\draw[ultra thick,-latex](2,2.25,4) -- (2,2.25,4.75) node[anchor=north west] {$\mathbf{a}_z$};
\draw [thick,->](4,0,0) arc (0:45:4 and 4.5);
\draw (3.6,2,0) node[anchor=north] {$\phi_1$};
\draw[ultra thick,-latex](0,0,0) -- (2,2.35,0);
\draw (1,1,0) node[anchor=north] {$r_1$};
\draw [ultra thick] (2,2.25,4)--(1.95,2.25,0);

\draw[ultra thick](0.1,0,4) -- (-0.1,0,4) node[anchor=south west] {$z_1$};
\end{tikzpicture}
\end{figure}
\end{document}


• @ferahfera - Thanks, is a good starting point for a cylindrical illustration. – Kiyoshi Feb 10 '14 at 11:06
• You use a circle for making the illustration, but I don't need a complete cylinder, how I can take only the part between the x axis and the y axis? – Kiyoshi Feb 10 '14 at 15:02
• @Kiyoshi, u can use 'arc' command. tex.stackexchange.com/q/66216/31034 – ferahfeza Feb 10 '14 at 18:18
• Thanks for the link but I can't do what I want. For now my knowledge about TikZ are insufficient. – Kiyoshi Feb 10 '14 at 19:16
• @ferahfeza CTAN is the official resource for LaTeX style files, a random repository on Google Code is not. TeX Live and MikTeX take their LaTeX packages from CTAN. The package I linked is the updated version of that 3dplot.sty, probably you already have tikz-3dplot in your distribution without manually downloading an old version from the Internet. – giordano Feb 15 '14 at 12:15

First, I want to give all the credit to @mrc . His work is quite good.

I want to complete a few details on his work. Basically he is missing two segments which makes sometimes the visualization difficult. For example the bottom of the cube, could look like the side towards the reader due to the missing two segments. Small details are arrows for angles increments, tilted text, smaller fonts, and different perspective, but again I am building on top of @mrc work.

 \begin{asy}[]
if(!settings.multipleView) settings.batchView=false;
import three;
settings.tex="pdflatex";
defaultfilename="input-1";
if(settings.render < 0) settings.render=40;
settings.outformat="";
settings.inlineimage=true;
settings.embed=true;
settings.toolbar=false;
defaultpen(fontsize(8pt));

size(9.5cm);
settings.outformat="pdf";
settings.prc=false;
import graph3;
import grid3;

currentprojection=orthographic(
camera=(2.9637389483254,1.48144594554777,1.03154384251566),
up=(-0.00245695858860577,0.00028972293310393,0.00664306468788549),
target=(0,0,0),
zoom=0.971150237157127);

//Draw Axes
pen thickblack = black+0.75;
real axislength = 1.0;
draw(L=Label("$x$", position=Relative(1.1), align=SW),
O--axislength*X,thickblack, Arrow3);
draw(L=Label("$y$", position=Relative(1.1), align=E),
O--axislength*Y,thickblack, Arrow3);
draw(L=Label("$z$", position=Relative(1.1), align=N),
O--axislength*Z,thickblack, Arrow3);

//Set parameters of start corner of polar volume element
real r = 0.8;
real q=0.25pi; //theta
real f=0.3pi; //phi

real dq=0.25; //dtheta
real df=0.25; //dphi
real dr=0.15;

triple A = r*expi(q,f);
triple Ar = (r+dr)*expi(q,f);
triple Aq = r*expi(q+dq,f);
triple Arq = (r+dr)*expi(q+dq,f);
triple Af = r*expi(q,f+df);
triple Arf = (r+dr)*expi(q,f+df);
triple Aqf = r*expi(q+dq,f+df);
triple Arqf = (r+dr)*expi(q+dq,f+df);

//label("$A$",A);
//label("$Ar$",Ar);
//label("$Aq$",Aq);
//label("$Arq$",Arq);
//label("$Af$",Af);
//label("$Aqf$",Aqf);
//label("$Arf$",Arf);
//label("$\; \; \quad Arqf$",Arqf);

pen thingray = gray+0.33;

draw(A--Ar);
draw(Aq--Arq);
draw(Af--Arf);
draw(Aqf--Arqf);
draw(O--Af, dashed);
draw(O--Aqf);
draw( arc(O,A,Aq) ,thickblack );
draw( arc(O,Af,Aqf),thickblack );
draw( arc(O,Ar,Arq) );
draw( arc(O,Arf,Arqf) );
draw( arc(O,Ar,Arq) );
draw( arc(O,A,Af),thickblack );
draw( arc(O,Aq,Aqf),thickblack );
draw( arc(O,Ar,Arf) );
draw( arc(O,Arq,Arqf));

pen thinblack = black+0.25;

//phi arcs
draw(O--expi(pi/2,f),thinblack);
draw("$\phi$", arc(O,0.5*X,0.5*expi(pi/2,f)),thinblack,Arrow3);
draw(O--expi(pi/2,f+df),thinblack);
draw( "$d\phi$", arc(O,expi(pi/2,f),expi(pi/2,f+df) ),thinblack , Arrow3);
draw( A.z*Z -- A,thinblack);
draw(L=Label(rotate(-5)*"$r\sin{\theta}$",position=Relative(0.5),align=N), A.z*Z -- Af,thinblack);

//cotheta arcs
draw( arc(O,Aq,expi(pi/2,f)),thinblack );
draw( arc(O,Aqf,expi(pi/2,f+df) ),thinblack);

//theta arcs
draw(O--A,thinblack);
draw(O--Aq,thinblack);
draw(L=Label("$\theta$",position=Relative(0.5), align=NE),
arc(O,0.25*length(A)*Z,0.25*A),thinblack,Arrow3);
draw(L=Label("$d\theta$",position=Relative(0.5),align=NE) ,arc(O,0.66*A,0.66*Aq),
thinblack, Arrow3 );

// inner surface
triple rin(pair t) { return r*expi(t.x,t.y);}
triple rout(pair t) { return 1.24*r*expi(t.x,t.y);}
surface inner=surface(rin,(q,f),(q+dq,f+df),16,16);
draw(inner,emissive(gray+opacity(0.33)));

// surface sider=surface(rin,(f,r),(f+df,r+dr),16,16);
// draw(sider,emissive(gray+opacity(0.33)));

//part of a nearly transparent sphere to help see perspective
surface sphere=surface(rout,(0,0),(pi/2,pi/2),26,26);
draw(sphere,emissive(gray+opacity(0.125)));

// dr and rdtheta labels
triple V= Af + 0.5*(Arf-Af);
draw(L=Label("$dr$",position=Relative(1.1)), V--(1.5*V.x,1.5*V.y,V.z),dotted);
triple U=expi(q+0.5*dq,f);
draw(L=Label("$rd\theta$",position=Relative(1.1)),
r*U ---r*(1.66*U.x,1.66*U.y,U.z),dotted );
\end{asy}

• Is it possible to publish a PDF version of that last picture (all vectorial) ? I would like to open it in my drawing app to edit it. – Cham Jul 19 '17 at 19:59
• Hello, I would like to add this figure in my notes on dynamic meteorology. However when I insert this code in my document I do not get any plot, except the following error "Latexmk: Missing input file: 'Meteodyn-1.pdf' from line 'Package asymptote Warning: file `Meteodyn-1.pdf' not found on input line 1002" and line 1002 happens to be \end{asy}. Is there something am missing? perhaps import hraph3, import grid3? I hope I can get some help. – Zilore Mumba Dec 24 '17 at 22:38
• I am sorry that I cannot help on this. I did this long time ago. I do not even have asymptote in my new computer. – Herman Jaramillo Dec 26 '17 at 1:44
• @Jaramillo finally I have been able to use your wonderful work. From other posts I learnt that I should have the file latexmkrc in the same directory as the .tex file I run. When I did this my file compiles. I really appreciate your work, thanks and best wishes. – Zilore Mumba Dec 26 '17 at 5:22
• Thank you very much. I am glad that my script helped in any way. – Herman Jaramillo Jan 16 '18 at 21:45

I decided to do this figure from a very different prospective. Instead of including the problem here, I will point to the link where I included the code and figure. 3D spherical volumen element