2

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}
Tony Mau
  • 419
  • 2
  • 5
  • 13

1 Answers1

4

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} 

enter image description here

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} 

enter image description here

percusse
  • 157,807