# Plot surface and normal vector fields

I was wondering if there is an easy way to plot a mesh diagram of a surface and also its normal vector field?

For example: I wish to plot the sphere $x^2 + y^2 + z^2 = 1$ and the normal vector field at will be all vectors joining (x,y,z) to (2x,2y,2z), where (x,y,z),lies on the sphere.tiks

• Welcome to TeX-SE!. Yes, there are possibilities. Since you tag your question tikz-pgf, you may want to have a look at quiver plots that come with pgfplots, which is based on TikZ. – user121799 Apr 24 '19 at 1:56
• However, I also feel obliged to tell you that TikZ does not have a real 3d engine. While pgfplots has some means of ordering elements of one surface, it does not do relative ordering of different 3d objects. If you want avoid doing things by hand, you may thus be interested in asymptote, see e.g. tex.stackexchange.com/a/212348/121799 for a nice application of that sort. – user121799 Apr 24 '19 at 2:26
• I will stop now spamming but here is an explicit example of a 3d vector plot of the type you seem to be looking for: tex.stackexchange.com/a/328086/121799 – user121799 Apr 24 '19 at 4:06
• Your spamming is much appreciated. Cheers! – user184701 Apr 25 '19 at 2:09

This is just for fun and builds on this answer for the background check as well as this answer.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}

\pgfplotsset{compute projections/.code=\pgfmathsetmacro{\CameraX}{sin(\pgfkeysvalueof{/pgfplots/view/az})*cos(\pgfkeysvalueof{/pgfplots/view/el})}
\pgfmathsetmacro{\CameraY}{-cos(\pgfkeysvalueof{/pgfplots/view/az})*cos(\pgfkeysvalueof{/pgfplots/view/el})}
\pgfmathsetmacro{\CameraZ}{sin(\pgfkeysvalueof{/pgfplots/view/el})},
only foreground/.style={compute projections,
restrict expr to domain={rawx*\CameraX + rawy*\CameraY + rawz*\CameraZ}{-0.05:100},
},only background/.style={compute projections,
restrict expr to domain={rawx*\CameraX + rawy*\CameraY + rawz*\CameraZ}{-100:0.05}
}}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
domain=-1:1,
samples=10,
xmin=-1.5,xmax=1.5,
ymin=-1.5,ymax=1.5,
zmin=-1.5,zmax=1.5,
]
domain=0:360,domain y=-90:90,point meta=1,
quiver={
u={x/sqrt((x)^2+(y)^2+(z)^2)},
v={y/sqrt((x)^2+(y)^2+(z)^2)},
w={z/sqrt((x)^2+(y)^2+(z)^2)},
colored,scale arrows=0.5}]
({cos(y)*cos(x)},{-cos(y)*sin(x)},{sin(y)});
%}
\draw[ball color=gray] let \p1=($(0,0,1.75)-(0,0,0)$) in