## 4. Vector Fields

To illustrate how to display vector data, we begin with a two dimensional vector
field that is the gradient of the function , which was used to illustrate contour,
raster and surface plots in the previous section. Code 3 illustrates how to write a simple
MTV data file containing the vector field (vx,vy,vz) = (2x,-2y,0) on a 25x25 grid over the
square 0 < x,y < 5. Note that even though a two dimensional vector field is to be
displayed, it is necessary to write the vector data set in the form x y z vx vy vz, where x,y,z
specify the tail of a given vector and vx,vy,vz designate the components of the vector.

program vector
parameter(mmax=25,nmax=25)
parameter(xmin=0.0,ymin=0.0,xmax=5.0,ymax=5.0)
parameter(dx=(xmax-xmin)/mmax,dy=(ymax-ymin)/nmax)
open(4,file='vect.dat',status='unknown')
c Specify that data are to be vectors
write(4,*)'$DATA=VECTOR'
c Set the maximum and minimum x, y and z-values
write(4,*)'%xmin=',xmin
write(4,*)'%ymin=',ymin
write(4,*)'%xmax=',xmax
write(4,*)'%ymax=',ymax
write(4,*)'%zmin=-0.5'
write(4,*)'%zmax=0.5'
c Set a scale factor to control the length of the vectors
write(4,*)'%vscale=0.02'
z = 0.0
vz = 0.0
y = ymin - dy
do 1 n = 1,nmax
x = xmin
y = y + dy
do 2 m = 1,mmax
vx = 2.0*x
vy = -2.0*y
c Write the coordinates of the tail and the components of vector
write(4,100)x,y,z,vx,vy,vz
x = x + dx
2 continue
1 continue
100 format(3f10.2,' ',3f10.2)
close(4)
end

Click here to have
plotmtv plot this vector field.

Figure 5 contains the result of using plotmtv with command line options -pfg
BLACK and -pbg WHITE to view the data set produced by Code 3.
Figure 5 demonstrates a problem one frequently
encounters when displaying vector data.
Namely, when vectors of markedly different magnitudes are present in the data set, it is
difficult to select a scale factor that makes the smallest of the vectors visible, yet prevents
the longer vectors from overlapping their neighbors. When this situation is encountered,
a number of techniques may help with the visualization of the data.

Figure 5 Plot of Vector Field (2x,-2y) from Code 3

One technique is to normalize the length of all the vectors to unity and then set the
line color of vectors in proportion to their magnitude. In such a scheme, the arrows show
the flow of the vector field and their colors indicate the "speed" of the flow. A second
technique, when plotting 2D vector fields, is to again normalize the magnitude of the vectors
and use the z component of the tail of the vector to represent the speed of the flow. Thus,
the vectors are displayed as horizontal arrows on a surface whose height represents the
magnitude of the vector. Yet another technique is to plot the magnitude of the vector field
as a contour or color raster map and then overlay normalized vectors indicating the
direction of flow.

The final illustration in this quick start is presented in Figure 6, which was created
by combining Codes 2 and 3 to write a file containing two MTV data sets. The first data
set contains the raster map of Figure 3, while the second data set contains the vector field
of Figure 5. Using the command line syntax

**plotmtv -pfg BLACK -pbg WHITE -plotall test1.dat**

permits the simultaneous display of the two data sets on a single plot.

Figure 6 Color Raster Map of x^2 - y^2 with the Vector Field (2x,-2y)