1

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 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Okay I see thanks! But I don't know I am trying to make a 3D grid.
Okay, I see; thanks! However, I don't know if I am trying to make a 3D grid. I want to define a function f(x,y,z) that will output a data file with six columns (x,y,z,Vx,Vy,Vz) where (x,y,z) represent 3D input points and (Vx,Vy,Vz) represent 3D vector coordinates. I've seen that it is possible to do this; all I need is the right data file; after that I should be able to use "splot <datafile> 1:2:3:4:5:6 with vectors" to plot the vector field. I hope you understand what I mean. (I tried to edit my comment but it didn't work.)
Here is a link to basically what I am trying to do: stackoverflow.com/questions/5666443/…. I want to define a function that can output a data file with 6 columns; then I should be able to use "splot" with relative ease.
Yes, I understood what you want, but gnuplot doesn't support 3D grids, you cannot plot a function of three variables f(x,y,z), but only f(x,y). In the case of vector fields you can get the third dimension via the for looping. I updated my answer to show a concrete example for the 3D vector field of a central force. That is the gnuplot-way of plotting this. If you want to generate a data file first, and plot this with gnuplot, that you should use an external script/program (e.g. python) to create this data file.
Okay, I see. You've really helped me a lot! Thanks!
1

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

3 Comments

Thanks a lot! That's exactly what I was trying to do. Do you know where or who would be able to assist me in defining vector fields?
Upvoting or marking as accepted would be nice if it was what you were looking for. For defining vector fields, maybe you better ask for help on mathematics

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.