# 3D spherical coordinate system in pgf plots

I just learned 3d rectangular graphs in pgf-plots, and now I need to graph in spherical coordinates. Ive been reading the manual and cant find an answer. Is there a simple way to plot something such as rho=1+sin(theta)*sin(phi). Thank you for the help.

\begin{tikzpicture}
\begin{axis}[
xmin=-5,xmax=5,
ymin=-5,ymax=5,
zmin=-5,zmax=5,
xlabel={$x$},
ylabel={$y$},
zlabel={$z$},
zlabel style={rotate=90},
view={75}{20}]
addplot3[surf,opacity=0.75,domain=-360:360,y domain=-360:360,samples=20]({x},{y},{1+0.2*sin(x)*sin(y)});
\end{axis}
\end{tikzpicture}

• Does that help ? Commented Jun 27, 2016 at 5:39
• that is kind of what im looking for, but the code is far beyond my level of understanding. Ive been using addplot3[] with pgf-plots and i was wondering if i could use the same command. Commented Jun 27, 2016 at 5:51
• What do you want to plot ? Can you try and give us your code (even if its bugged) ? Commented Jun 27, 2016 at 6:30
• my code is pretty much inexistant. the equation above is what im trying to plot. rho=1+sin(theta)*sin(phi) Commented Jun 27, 2016 at 7:46
• \begin{tikzpicture} \begin{axis}[ xmin=-5,xmax=5, ymin=-5,ymax=5, zmin=-5,zmax=5, xlabel={$x$}, ylabel={$y$}, zlabel={$z$}, zlabel style={rotate=90}, view={75}{20}] addplot3[surf,opacity=0.75,domain=-360:360,y domain=-360:360,samples=20]({x},{y},{1+0.2*sin(deg(x))*sin(deg(y))}); \end{axis} \end{tikzpicture} Commented Jun 27, 2016 at 7:46

## 1 Answer

You can use the parametric equations in the regular syntax

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.13,colormap/viridis}
\begin{document}
\begin{tikzpicture}
\begin{axis}[trig format=rad,view={40}{45}]
\addplot3+[no marks,mesh,domain=-pi/2:pi/2,y domain=0:2*pi,samples=61,samples y=61]
({cos(x)*cos(y)},{cos(x)*sin(y)},{1 + 0.2*sin(x)*sin(y)});
\end{axis}
\end{tikzpicture}
\end{document}


For further complications, basically, you have to either make a mini parser or send it to a more powerful syntax handler for which gnuplot is a very good alternative. You have to make sure that gnuplot is on the system path and your editor calls TeX with -shell-escape (or -enable-write18 on Windows) option to communicate with gnuplot.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13,colormap/viridis}
\begin{document}
\begin{tikzpicture}
\begin{axis}%[view={-40}{45}]
\addplot3+[no marks,mesh,raw gnuplot,samples=] gnuplot {
r = 1;
set samples 101;
set parametric;
set urange [0:2*pi];
set vrange [-pi/2:pi/2];
fx(v,u) = r*cos(v)*cos(u);
fy(v,u) = r*cos(v)*sin(u);
fz(v,u) = r + 0.2*sin(u)*sin(v);
splot fx(v,u),fy(v,u),fz(v,u);
};
\end{axis}
\end{tikzpicture}
\end{document}