Plotting a function directly from a text file

awk, gnuplot

Solution

Another possibilty to have a somewhat generic version for this, you can do the following:

Assume, the parameters are stored in a file `parameters.dat` with the first line containing the variable names and all others the parameter sets, like

location a b c
1        1 3 4

The script file looks like this:

file = 'parameters.dat'
par_names = system('head -1 '.file)
par_cnt = words(par_names)

# which parameter set to choose
par_line_num = 2
# select the respective string
par_line = system(sprintf('head -%d ', par_line_num).file.' | tail -1')
par_string = ''
do for [i=1:par_cnt] {
  eval(word(par_names, i).' = '.word(par_line, i))
}
f(x) = a*x**2 + b*x + c

plot f(x) title sprintf('location = %d', location)

Problem

Is there a way to plot a function based on values from a text file? I know how to define a function in gnuplot and then plot it but that is not what I need. I have a table with constants for functions that are updated regularly. When this update happens I want to be able to run a script that draws a figure with this new curve. Since there are quite few figures to draw I want to automate the procedure. Here is an example table with constants: ``` location a b c 1 1 3 4 2 ``` There are two ways I see to solve the problem but I do not know if and how they can be implemented. - I can then use awk to produce the string: `f(x)=1(x)**2+3(x)+4`, write it to a file and somehow make gnuplot read this new file and plot on a certain `x` range. - or use awk inside gnuplot something like `f(x) = awk /1/ {print "f(x)="$2` etc., or use awk directly in the plot command. I any case, I'm stuck and have not found a solution to this problem online, do you have any suggestions?

Original source

Related problems