1
votes

I am new to gnuplot. I am trying to plot 3d vector fields. I know that I have to create a data file that has six columns representing x, y, z, deltax,deltay, deltaz.

I wondering if it is possible to define a function f(x,y,z) and have gnuplot create the data file I desire in order to plot the vectors. If so, how do I go about doing this?

2

2 Answers

2
votes

You can use the special file name ++ to create a 2d grid on which you can define you vector field:

set xrange [-10:10]
set yrange [-10:10]
set samples 100
set isosamples 100

vx(x,y,z) =  ...
vy(x,y,z) = ...
vz(x,y,z) = ...
z(x,y) = ...

splot '++' using 1:2:(z($1,$2)):(vx($1,$2,z($1,$2))):(vy($1,$2,z($1,$2))):(vz($1,$2,z($1,$2))) with vectors

This plots a vector field (vx,vy,vz) on the surface z(x,y). Gnuplot cannot generate a 3d grid.

You could try to simulate such a real 3d vector field with a loop:

z(i) = -10 + i*(20.0/99.0)
splot for [i=1:100] '++' using 1:2:(z(i)):(vx($1,$2,z(i))):(vy($1,$2,z(i))):(vz($1,$2,z(i))) with vectors lt 1

As example considert the following script for a central force:

lim = 2
N = 6

set xrange [-lim:lim]
set yrange [-lim:lim]
set zrange [-lim:lim]
set samples N
set isosamples N

sc= 0.3
r(x,y,z) = sqrt(x**2 + y**2 + z**2)
gx(x,y,z) = sc/(x**2 + y**2 + z**2) * x/r(x,y,z)
gy(x,y,z) = sc/(x**2 + y**2 + z**2) * y/r(x,y,z)
gz(x,y,z) = sc/(x**2 + y**2 + z**2) * z/r(x,y,z)

z(i) = -lim + 2*i*lim/(N - 1.0)

unset key
set xyplane 0
splot for [i=1:N] '++' using 1:2:(z(i)):(gx($1,$2,z(i))):(gy($1,$2,z(i))):(gz($1,$2,z(i))) with vectors lt 1

with the output

enter image description here

1
votes

Here an answer that would apply to a 3d surface plot. I'm sure this can be expanded to work for a vector field as well - a matter of writing the function which is outside my comfort zone.

You would plot your function to a table rather than a graphic output and then plot the graph using the table you just have produced (and could inspect, edit etc. before). Sample:

# output to a file with the data
set table "so.dat"

# plot your function
splot [-2:2][-2:2] exp(-(x**2 + y**2))*cos(x/4)*sin(y)*cos(2*(x**2+y**2))

# switch the graphic screen on again and plot from your file
unset table
unset ztics                       # avoiding cluttered ztics
splot "so.dat" u 1:2:3 w l

yields

enter image description here

Sample taken from Philipp K. Janert, Gnuplot In Action