A vector field attaches a small arrow to each point of a grid, showing the direction and strength of a flow, a force, or a gradient at a glance.
$FIELD << EOD
-2 -1 0.3 -0.6
-1 -1 0.3 -0.3
0 -1 0.3 0.0
1 -1 0.3 0.3
2 -1 0.3 0.6
-2 0 0.0 -0.6
-1 0 0.0 -0.3
0 0 0.0 0.0
1 0 0.0 0.3
2 0 0.0 0.6
-2 1 -0.3 -0.6
-1 1 -0.3 -0.3
0 1 -0.3 0.0
1 1 -0.3 0.3
2 1 -0.3 0.6
EOD
set title "Vector Field of (-y, x), Scaled by 0.3"
set xlabel "x"
set ylabel "y"
set xrange [-2.6:2.6]
set yrange [-1.6:1.6]
unset key
plot $FIELD using 1:2:3:4 with vectors head filled size 0.12,18 linecolor rgb "#5e2d79" linewidth 1.5
How the script works
Each row of the datablock is one arrow: x, y, dx, dy. The sample values encode the rotation field (-y, x) scaled by 0.3, so at the point (2, 1) the arrow is (-0.3, 0.6) — you can check any row by hand with that formula, which is exactly how you would generate a larger grid from your own function.
using 1:2:3:4 with vectors reads the four columns as base point plus delta: the arrow starts at (column 1, column 2) and extends by (column 3, column 4). The deltas are in data units, not screen units, so longer deltas mean stronger field values.
The style clauses shape the arrows: head filled draws solid arrowheads, size 0.12,18 sets head length and opening angle, and linecolor / linewidth style the shaft. The widened xrange and yrange leave room so arrows near the edge are not clipped.
Run it online: Paste this script into the online terminal and run it — no install needed.
Variant to try
To sample a function field without writing rows by hand, gnuplot can sample the pseudo-file '+': for a field such as (x, -y) on a coarse grid, a common pattern is plot '+' using 1:2:($1*0.15):(-$2*0.15) every 3::... with vectors — in practice the datablock form above is easier to control and debug, because every arrow is visible in the script. For data from a simulation file, the same using 1:2:3:4 with vectors line works unchanged on the file.
Related tutorials: Line Styles, Colors and Point Types, Your First Gnuplot Plot. See more copy-and-run scripts in the Gnuplot Examples index.
Scripts on this page are adapted from the official gnuplot 6.0 demo collection (gnuplot.info) and rewritten for this tutorial.
