# How to draw a 3D sphere and a 3D cone for Spherical coordinates

I'm trying to replicate this figure:

Right know, I have this code:

\begin{figure}[H]
\centering
\begin{tikzpicture}[yzx, scale = 0.9]
\draw[->] (0,0,0) -- (5.5,0,0) node(x)[left]{$x$};
\draw[->] (0,0,0) -- (0,5,0) node(y)[right]{$y$};
\draw[->] (0,0,0) -- (0,0,4) node(z)[right]{$z$};

\def\px{4}; \def\py{4}; \def\pz{4};
\coordinate (o) at (0,0,0);
\coordinate (p) at (\px,\py,\pz);
\coordinate (pxy) at (\px,\py,0);

% Spherical coordinates
\begin{scope}[canvas is xy plane at z=0]
\draw[->, cadmiumorange] (2,0) arc(0:45:2) node[pos = 0.5, below]{$\phi$};

\draw[dotted, cadmiumorange] (o) -- (pxy);
\end{scope}

\draw[cadmiumorange] (o) -- (p) node[above left, pos = 0.5]{$r$};

\draw[dotted] (pxy) -- (p) node[pos = 0.5, right]{$z$};

\begin{scope}[canvas is zy plane at x=0]
\draw[->, cadmiumorange] (1,0) arc(0:45:1) node[pos = 0.5, above]{$\theta$};
\end{scope}

% x-y-z coordinates
\draw[bleudefrance] (o) -- (\px,0,0) node[above, pos = 0.5, sloped]{$x$};

\draw[bleudefrance] (\px,0,0) -- (pxy) node[pos = 0.5, below]{$y$};

% Point
\filldraw (p) circle(3pt)
node[above right]{$P(r, \theta, \phi)$}
node[below right]{$P(x,y,z)$};
\end{tikzpicture}
\caption{Coordenadas cartesianas y esf\'ericas.}
\label{c2:definiciones:esfericasB}
\end{figure}


that produces

That's all I'm able to draw by my own. I don't know how to draw this "solids" sphere and cone. The text in the first figure is in spanish but it's not needed in the answer.

The ideal asnswer to this question would be an answer like the answer of this question: How to draw the solids to refer to cylindricals coordinates. This answer is (relatively) easy to understand and modify.

I also have this in my code:

\documentclass[11pt, oneside]{book}

\usepackage{pgfplots}

\usepackage{tikz-3dplot}
\usepackage{tikz}
\usetikzlibrary{snakes, calc, quotes, babel, hobby, trees, arrows, patterns, patterns.meta, calligraphy, backgrounds, chains, shapes.geometric, shapes, angles, 3d, fillbetween,
decorations, decorations.pathreplacing,
decorations.pathmorphing, decorations.markings, decorations.text}

\tikzstyle{yzx} = [
x={(-.385cm, -.385cm)},
y={(1cm, 0cm)},
z={(0cm, 1cm)},
]
\tikzset{zxplane/.style={canvas is zx plane at y=#1,very thin}}
\tikzset{yxplane/.style={canvas is yx plane at z=#1,very thin}}

\usepackage{tkz-euclide}
\tikzset{every picture/.style = {line width = 1pt, > = Triangle},
%
%
%
set arrow inside/.code = {\pgfqkeys{/tikz/arrow inside}{#1}},
set arrow inside={end/.initial = >, opt/.initial =},
/pgf/decoration/Mark/.style = {
mark/.expanded = at position #1 with {
\noexpand\arrow[\pgfkeysvalueof{/tikz/arrow inside/opt}]{\pgfkeysvalueof{/tikz/arrow inside/end}}
}
},
arrow inside/.style 2 args = {
line width = 2pt,
set arrow inside = {#1},
postaction = {
decorate, decoration={
markings, Mark/.list = {#2}
}
}
},
}

\usepackage{physics}
\usepackage[italic = true]{derivative}
\usepackage[scr = rsfso]{mathalfa}
\usepackage{mathtools}
\usepackage{amssymb}



Thanks for reading and keep helping!

• Take a look at this post: tex.stackexchange.com/questions/586948/… Feb 22, 2023 at 18:55
• Well, that's an amazing draw but is far too complex for me. Any way to make it simplier or all of these draws are like this, complex? With "complex" I mean too many lines of code Feb 22, 2023 at 19:40
• Unfortunately tikz is a massive learning curve but fortunately it is extremely powerful and worth investing the time in (I am currently learning it). Your original image looks quite complicated indeed, I'd advise going through the tikz manual and trying to replicate parts at a time. I doubt anyone would reproduce it for you unless they were bored to be honest, that's how "complex tikz drawing" questions tend to go Feb 22, 2023 at 19:48
• @Peluche, as JamesT says, TikZ could be difficult at the beginning but is quite useful. See my answer, with some tips about your drawing. Feb 22, 2023 at 20:01

You can draw something like your picture essentially using the canvas is... options from 3d library. For example:

\tikzset
{
xz/.style={canvas is xz plane at y=0},
xz rotated/.style={rotate around z=\mytheta,xz},
}


Here I'm defining two styles. The first one draw in the xz plane (if you choose a different y then draws in a parallel plane at distance y). The second draws in another plane, obtained rotating the xy plane around the z-axis \mytheta degrees (defined to be 60).

Using these new planes you only need to draw a few straight lines and a couple of arcs, all of them related to the angles \mytheta and \myphi.

A complete example (well... I left the labels for you) could be:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{3d} % for 'canvas is...' options

\tikzset
{% styles
axis/.style={thick,-latex},
yz/.style={canvas is yz plane at x=0},
xz/.style={canvas is xz plane at y=0},
xz rotated/.style={rotate around z=\mytheta,xz},
xy elevated/.style={canvas is xy plane at z={\myradius*cos(\myphi)},scale={sin(\myphi)}},
plane/.style={fill=teal,fill opacity=0.3},
cone/.style={fill=yellow,fill opacity=0.3},
vector/.style={very thick,-stealth},
}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,
x={(-0.4cm,-0.4cm)}, y={(1cm,0)},z={(0,1cm)}]
% dimensions
\def\mytheta{60}
\def\myphi{30}
% axes
\draw[axis] (0,0,0) -- (1.3*\myradius,0,0) node[below] {$x$};
\draw[axis] (0,0,0) -- (0,1.3*\myradius,0) node[right] {$y$};
\draw[axis] (0,0,0) -- (0,0,1.3*\myradius) node[above] {$z$};
% sphere
% plane
\draw[plane,xz rotated] (0,\myradius) arc (90:0:\myradius) -| cycle;
% cone
\draw[cone] {[xy elevated] (\myradius,0) arc (0:\mytheta:\myradius) coordinate (P)} -- (0,0,0) -- cycle;
% other arcs
\draw[-latex] (1,0,0) arc (0:\mytheta:1) node[midway,below] {$\theta$};
\draw[xz rotated,-latex] (0,2) arc (90:90-\myphi:2) node[midway,above] {$\varphi$};
% vectors
\draw[xz rotated ,vector] (P) --++ (90-\myphi:1.5) node[above] {$\hat{r}$};
\draw[xz rotated ,vector] (P) --++ (-\myphi:1.5)   node[right] {$\hat{\theta}$};
\draw[xy elevated,vector] (P) --++ (90+\mytheta:3) node[right] {$\hat{\varphi}$};
% point
\fill[point] (P) circle (0.1cm);
\end{tikzpicture}
\end{document}


• In your code, you're defining a new arc that starts in the plane yz at x=0 with this code: {[yz] arc (0:90:\myr)} ? It's in your sphere section. I don't understand this brace Feb 22, 2023 at 21:03
• @Peluche it is likely due to tikz and the way it processes square brackets i.e. []. In forest, a tikz derived package, square brackets must be put in {} when they are used as optional arguments for commands so I am assuming it is similar here Feb 22, 2023 at 22:10
• @Peluche these brackets (and others) apply the style, drawing in xy plane, only for the part of the path enclosed inside them Feb 23, 2023 at 6:28