# How to plot vector field in Latex using tikz

A vector field is an assignment of a vector to each point in a subset of space with a given magnitude and direction. In this tutorial, we will learn how to draw vector field in 2-dimensional coordinates using TikZ and Pgfplots packages.

A vector field is a set of vectors in space, which every coordinate $(x,y)$ has a vector of components $[u,v]$. The components of the vectors are functions of the coordinates, it means we can write the vector field as a mathematical expressions of the variables $x$ and $y$: $$F = \begin{cases} u = f(x,y) \\ v = g(x,y) \end{cases}$$

TikZ provides the $\verb|quiver|$ option, which is a very useful way to plot vector fields. You may have heard about something similar if you are using Matlab.  Let’s see how it is working in $\LaTeX$ !

## 1. Create tikzpicture environment

First you have to set up the $\LaTeX$ document where we need to use TikZ and Pgfplots packages.
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

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


## 2. Plot vector field in $\LaTeX$

Consider the following example: $$F = \begin{cases} u = – y \\ v = x \end{cases}$$ The following code shows how the option $\verb|quiver|$ is used to plot vector field of the above example:
\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = -4, xmax = 4,
ymin = -4, ymax = 4,
zmin = 0, zmax = 1,
axis equal image,
view = {0}{90},
]
quiver = {
u = {-y},
v = {x},
},
-stealth,
domain = -4:4,
domain y = -4:4,
] {0};
\end{axis}
\end{tikzpicture}
\end{document}

Notice the $\verb|quiver|$ option is just a modification of the $\verb|\addplot3|$ command. In general the $\verb|\addplot3|$ command is used to plot 3D surfaces, but we can modify it to get 2D representation of a vector field by writing down $\verb|{0}|$ at the end of the command. This means we are plotting the vector field in the $z=0$ plane, which is precisely the $xy$ plane.
The $\verb|-stelth|$ option defines the type of arrow we want for the vectors, the options available are $\verb|->|$ and $\verb|-latex|$.

The obtained illustration after compiling this code is shown above. This vector field looks like a twister. Here we have settled a domain from $-4$ to $4$. It’s important to set the visualisation to $\verb|view = {0}{90}|$, since we are plotting a 2D vector field, if you use another values for the $\verb|view|$ command you will get unexpected results.

As you see, plotting a vector field is pretty easy. however, the result is quite far from being a classy plot of a vector field. We can remark that the vectors are intersecting, and for better visualisation we should normalise these vectors in order to avoid nasty intersections. And this the objective of the next section!

## 3. Normalise vectors

To normalise means to divide each vector by its modulus (the length of the vector). In this case the modulus of each vector is defined by: \begin{aligned} \parallel F \parallel & = \sqrt{(-y)^2+(x)^2}\\ & = \sqrt{x^2+y^2} \end{aligned} So if we divide each vector by its modulus, we will get a better representation of the vector field. Check the following code to get an idea about how we can do that in $\LaTeX$.
\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = -4, xmax = 4,
ymin = -4, ymax = 4,
zmin = 0, zmax = 1,
axis equal image,
view = {0}{90},
]
quiver = {
u = {-y/sqrt(x^2+y^2)},
v = {x/sqrt(x^2+y^2)},
scale arrows = 0.25,
},
-stealth,
domain = -4:4,
domain y = -4:4
] {0};
\end{axis}
\end{tikzpicture}
\end{document}

Sometimes vectors are bigger than what we would like, for this reason it is recommended that we add a scaling factor to vectors by using the $\verb|scale arrows|$ command.
The obtained result is shown above. Now the vector field looks better, but we have lost some information about the real length of vectors. To solve this it is usual to use a $\verb|colormap|$ to show the length of the vectors.

## 4. Add colormap to vector field

To use a colormap for the plot, we need to load the colormaps library in the preamble: $\verb|\usepgfplotslibrary{colormaps}|$. This will load the default colormaps, so you can use them in your plots by writing $\verb|colormap/color_map_name|$.
\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = -4, xmax = 4,
ymin = -4, ymax = 4,
zmin = 0, zmax = 1,
axis equal image,
xtick distance = 1,
ytick distance = 1,
view = {0}{90},
scale = 1.25,
title = {\bf Vector Field $F = [-y,x]$},
xlabel = {$x$},
ylabel = {$y$},
colormap/viridis,
colorbar,
colorbar style = {
ylabel = {Vector Length}
}
]
point meta = {sqrt(x^2+y^2)},
quiver = {
u = {-y/sqrt(x^2+y^2)},
v = {x/sqrt(x^2+y^2)},
scale arrows = 0.25,
},
quiver/colored = {mapped color},
-stealth,
domain = -4:4,
domain y = -4:4,
] {0};
\end{axis}
\end{tikzpicture}
\end{document}


The code shows how to implement the colormap and how to plot the colorbar by using these options in the $\verb|\addplot3|$ command:

• $\verb|point meta = {sqrt(x^2+y^2)}|$: This option sets the colour of each vector in function of its length.
• $\verb|quiver/colored|$: activates the colormap for the plot, if you don’t use this option you will generate a single colour vector field.

And for the $\verb|axis|$ environment we use these options:

• $\verb|colormap/viridis|$: here you can use any of the default colormaps provided by $\verb|pgfplots|$ like $\verb|viridis|$, $\verb|greenyellow|$, $\verb|jet|$, $\verb|hot|$ and so on.
• $\verb|colorbar|$: Shows a colorbar code at the right of the plot.
Also we have added labels for the axis and a title for the draw.

Using this example as a reference you can plot any vector field and use a colormap to make it look attractive without losing information of the length of each vector.