Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Gnuplot take different arguments at run time? maybe with Python?

I have 500 files to plot and I want to do this automatically. I have the gnuplot script that does the plotting with the file name hard coded. I would like to have a loop that calls gnuplot every iteration with a different file name, but it does not seem that gnuplot support command line arguments.

Is there an easy way? I also installed the gnuplot-python package in case I can do it via a python script.However, I couldn't find the api so it's a bit difficult to figure out.

Thank you!

like image 908
user431336 Avatar asked Dec 07 '10 16:12

user431336


People also ask

How does gnuplot work?

Gnuplot makes a graph of any kinds of functions or numerical data stored in a file. To plot a function, use the plot/splot command with a range of X-axis (or X and Y ranges for 3-dim. plot) and the function. You can omit the range parameters.

How to plot a function using gnuplot?

To plot functions simply type: plot [function] at the gnuplot> prompt. Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes) on the plot or splot command line. Data files should have the data arranged in columns of numbers.


2 Answers

You can transform your gnuplot script to a shell script by prepending the lines

#!/bin/sh
gnuplot << EOF

appending the line

EOF

and substituting every $ by \$. Then, you can substitute every occurence of the filename by $1 and call the shell script with the filename as parameter.

like image 114
Sven Marnach Avatar answered Oct 27 '22 10:10

Sven Marnach


Regarding the $'s in Sven Marnach's solution (the lines between EOF are called a "here script" in shell parlance): in my experience, one uses shell variables as usual, but $s that are meant for gnuplot itself must be escaped.

Here is an example:

for distrib in "uniform" "correlated" "clustered"
do
    gnuplot << EOF
    # gnuplot preamble omitted for brevity

    set output "../plots/$distrib-$eps-$points.pdf"
    set title "$distrib distribution, eps = $eps, #points = $points"

    plot "../plots/dat/$distrib-$eps-$points.dat" using 1:(\$2/$points) title "exact NN"
EOF

done

Note the backslash escaping the dollar so that gnuplot sees it.

like image 33
Gabriel Avatar answered Oct 27 '22 11:10

Gabriel