Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a function directly from a text file

Tags:

awk

gnuplot

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.

  1. 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.
  2. 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?

like image 479
Bergur Sigfusson Avatar asked Feb 21 '13 16:02

Bergur Sigfusson


People also ask

How do you plot data from a text file?

MatPlotLib with PythonOpen a sample . txt file in read "r" mode and append to bar's name and height list. Make a bar plot. To display the figure, use show() method.


1 Answers

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)
like image 74
Christoph Avatar answered Sep 18 '22 20:09

Christoph