# Velocity Field / 3D Vector Fields in Tikz or PGFplots

I would like to draw "Velocity field" or "Vector Field" like:

I have tried using PGFplots "quiver" but this only works for vector fields on SURFACES:

\begin{tikzpicture}
\begin{axis}[
domain=0:1,
xmax=1,
ymax=1,
]
quiver/u=y,
quiver/v=z,
quiver/w=x,
quiver/scale arrows=0.1,
-stealth,samples=10] ({x},{y},{x+y});
\end{axis}
\end{tikzpicture}


Is there a mechanism for drawing vectors on a 3D LATTICE? Something which does

for i from 1 to 10
for j from 1 to 10
for k from 1 to 10
draw vector (i,j,k) -- f(i,j,k);
end do;
end do;
end do;


in PGFplots or Tikz? (As done here: 3D Vector Fields in Asymptote )

• Is there some reason you don't want to use Asymptote? Unlike PGF/TikZ, Asymptote knows about 3D. (3D in PGF/TikZ is 2D pretending to be 3D, which is why drawing order matters, for example.) – cfr Sep 5 '16 at 10:09
• – cfr Sep 5 '16 at 10:20
• I have no experience with Asymptote. Was hoping there would be an "in house" solution. – vrbatim Sep 5 '16 at 11:20

You could unroll the layers in z direction by hand using \pgfplotsinvokeforeach like in the hedgehog example below. I could not use your example because the parametric function f = (x,y,x+y) actually is a surface.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
domain=-1:1,
samples=10,
xmin=-1,xmax=1,
ymin=-1,ymax=1,
zmin=-1,zmax=1,
]
\pgfplotsinvokeforeach{-1,-.5,0,.5,1}{
point meta={sqrt((x)^2+(y)^2+(z)^2)},
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=.1}]
(x,y,#1);
}
\end{axis}
\end{tikzpicture}

\end{document}


• Very impressive! I'm relieved that my attempt with the equation from the question was not totally wrong-headed. I thought I must be missing something when it didn't look three dimensional. – cfr Sep 5 '16 at 16:24

\documentclass[margin=1cm]{standalone}
\usepackage[pdftex]{graphicx}
\usepackage{pgfplots,tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{decorations.markings,arrows}
\usepackage{amsmath}

\tdplotsetmaincoords{60}{120}

\begin{document}

\begin{tikzpicture}[scale=1.4]
% scale
\pgfmathsetmacro{\s}{0.15}

\foreach \i in {-0.8,-0.6,...,0.8}
\foreach \j in {-0.8,-0.6,...,0.8}
\foreach \k in {-0.8,-0.6,..., 0.8}
\draw[->, color=cyan, line width=0.2pt]
(\i, \j, \k) -- (\i+ \s*\j, \j + \s*\k, \k  + \s*\i);

% draw the cube
\draw[] (-1,1,1)--(1,1,1);
\draw[] (-1,1,-1)--(1,1,-1);
\draw[] (-1,1,1)--(-1,1,-1);
\draw[] (1,1,1)--(1,1,-1);

\draw[] (-1,-1,1)--(1,-1,1);
\draw[] (-1,-1,-1)--(1,-1,-1);
\draw[] (-1,-1,1)--(-1,-1,-1);
\draw[] (1,-1,1)--(1,-1,-1);

\draw[] (-1,1,1)--(-1,-1,1);
\draw[] (-1,1,-1)--(-1,-1,-1);
\draw[] (-1,1,1)--(-1,-1,1);
\draw[] (1,1,1)--(1,-1,1);
\draw[] (1,1,-1)--(1,-1,-1);

\end{tikzpicture}
\end{document}


And the figure