# How to draw and annotate a sphere in LaTeX using Tikz

In this tutorial, We will draw a shaded sphere centred at the origin of a coordinate system and locate a vector from the origin to some point on the surface of the sphere.

## 1. Create tikzpicture environment

First you have to set up the LaTeX document following the next code.
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{60}{115}

\begin{document}
\begin{tikzpicture}[]
% Here goes the code
\end{tikzpicture}
\end{document}


Standalone is used as a documentclass, which allows us get an output file that only contains the picture with no extra margins. The main package used is TikZ, but we also need to load the supporting packages pgfplots and tikz-3dplot. The first one is useful to draw shades and gradients and the second one will help us to rotate objects and to change the angle of visualisation.

In our example, we’ve used \tdplotsetmaincoords{60}{115} to set up an accurate angle of visualisation. However, you can put different values to get a different point of view of the 3D drawing.

## 2. Draw axes with \draw command

The next code shows how to use the \draw command,  the most important command in TikZ. This command is used to draw lines, arrows, circles, and any basic shape. Depending on the [options] you use in the command, you can get different types of lines like dashed or dotted, and also you can specify the stroke of the line like thin or thick. For instance, if you write [-stealth] in the options you will get a line with an arrowhead.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{60}{115}

\begin{document}
\begin{tikzpicture}[tdplot_main_coords, scale = 2.5]
\coordinate (P) at ({1/sqrt(3)},{1/sqrt(3)},{1/sqrt(3)});

\draw[dashed, gray] (0,0,0) -- (-1,0,0);
\draw[dashed, gray] (0,0,0) -- (0,-1,0);

\draw[-stealth] (0,0,0) -- (1.80,0,0);
\draw[-stealth] (0,0,0) -- (0,1.30,0);
\draw[-stealth] (0,0,0) -- (0,0,1.30);
\draw[thick, -stealth] (0,0,0) -- (P);

\draw[thin, dashed] (P) --++ (0,0,{-1/sqrt(3)});
\draw[thin, dashed] ({1/sqrt(3)},{1/sqrt(3)},0) --++
(0,{-1/sqrt(3)},0);
\draw[thin, dashed] ({1/sqrt(3)},{1/sqrt(3)},0) --++
({-1/sqrt(3)},0,0);
\end{tikzpicture}
\end{document}


## 3. Use \coordinate command to define points

\coordinate command is used to define a point on the surface of a sphere with radius $1$. This point is named $P$ and has the 3D coordinates $(1/\sqrt{3},1/\sqrt{3},1/\sqrt{3})$

In tikzpicture environment options, we have added tdplot_main_coords in order to make sure that we are using the angles of visualisation defined previously. In addition, you can specify the scale of the picture by using the scale command, here we use a factor of $2.5$. The following annotated picture regroups different commands used in the next code block.

## 4. Draw arc and circle in 3D coordinates

The next step is very simple. We have to draw arcs to make a wireframe structure of the sphere. For this purpose, we use again the \draw command, but also we have to use the \tdplotsetrotatedcoords{a}{b}{c} command, which allows us to rotate the arcs around the coordinate axes as follows:

\tdplotsetrotatedcoords{0}{0}{0};

\draw[dashed,tdplot_rotated_coords] (0,0,0) circle (1);

\tdplotsetrotatedcoords{90}{90}{90};

\draw[dashed,tdplot_rotated_coords] (1,0,0) arc (0:180:1);

\tdplotsetrotatedcoords{0}{90}{90};

\draw[dashed,tdplot_rotated_coords] (1,0,0) arc (0:180:1);


Check how you have to choose the correct angles of rotation to get the desired shapes. This can be confusing at the beginning, but once you learn how to use it correctly is very easy and useful.

Now it comes the magic. We have until now a wireframe sphere, but it looks plain. Therefore, to make it look as a true sphere we can use a shaded effect which is actually a trick since we are not plotting any sphere for real but an illusion of a sphere. To achieve that, we use the command \shade as below.

\shade[ball color = lightgray, opacity = 0.5] (0,0,0) circle (1cm);

## 6. Add labels with node [position] {text}

Finally, to annotate the drawing and locate the labels of axis and points we can use the node[location] {label} command after each coordinate to be annotated.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{60}{115}

\begin{document}
\begin{tikzpicture}[tdplot_main_coords, scale = 2.5]
\coordinate (P) at ({1/sqrt(3)},{1/sqrt(3)},{1/sqrt(3)});

\shade[ball color = lightgray, opacity = 0.5] (0,0,0) circle (1cm);

\tdplotsetrotatedcoords{0}{0}{0};
\draw[dashed, tdplot_rotated_coords, gray] (0,0,0) circle (1);

\tdplotsetrotatedcoords{90}{90}{90};
\draw[dashed, tdplot_rotated_coords, gray] (1,0,0) arc (0:180:1);

\tdplotsetrotatedcoords{0}{90}{90};
\draw[dashed, tdplot_rotated_coords, gray] (1,0,0) arc (0:180:1);

\draw[dashed, gray] (0,0,0) -- (-1,0,0);
\draw[dashed, gray] (0,0,0) -- (0,-1,0);

\draw[-stealth] (0,0,0) -- (1.80,0,0) node[below left] {$x$};
\draw[-stealth] (0,0,0) -- (0,1.30,0) node[below right] {$y$};
\draw[-stealth] (0,0,0) -- (0,0,1.30) node[above] {$z$};
\draw[thick, -stealth] (0,0,0) -- (P) node[right] {$P$};

\draw[thin, dashed] (P) --++ (0,0,{-1/sqrt(3)});
\draw[thin, dashed] ({1/sqrt(3)},{1/sqrt(3)},0) --++
(0,{-1/sqrt(3)},0);
\draw[thin, dashed] ({1/sqrt(3)},{1/sqrt(3)},0) --++
({-1/sqrt(3)},0,0);

\draw[fill = lightgray!50] (P) circle (0.5pt);
\end{tikzpicture}

\end{document}


And we are done with the drawing of a 3D sphere. This code is useful when you have to sketch spherical coordinate systems or related drawings!