How to plot a 2 dimensional function using data stored in a file with gnuplot

915 Views Asked by At

Is there any way to compute a 2 dimensional function using data from a file in gnuplot. Suppose I have a function f(x,y) which exist and I want to calculate the new values with data stored in file data.dat

i.e something like

plot f(x,y) using 'data.dat'$1:'data.dat'$2
1

There are 1 best solutions below

1
brm On

The command plot is used for plotting one variable against another. If you want to plot a third value against two others (and obtain something that looks 3D), you'll need the splot command. In this case, the command would look like

splot 'mydata.txt' using 1:2:(f($1,$2))

The using keyword specifies what you want to plot based on the contents of a file. The 1 and 2 means that the x and y coordinates will just be the first and second column in the file. For the third coordinate we want the f(x,y) function to be used with the values from the first and second column filled in ($1 and $2).

In case we're doing something more complex than just using a column unmodified, we have to use brackets and a $-sign for the variables. So we also could have written

splot 'mydata.txt' using ($1):($2):(f($1,$2))

as the command. See the gnuplot manual for more information.