Loop structure inside gnuplot?

Is there a way to iteratively extract data from multiple files and build them on the same graph in gnuplot. Suppose I have files like data1.txt, data2.txt ...... data1000.txt; each of which has the same number of columns. Now I could write something like:

plot "data1.txt" using 1:2 title "Flow 1", \ "data2.txt" using 1:2 title "Flow 2", \ . . . "data1000.txt" using 1:2 title "Flow 6" 

But that would be really inconvenient. I was wondering if there is a way to scroll the plot part in gnuplot.

+70
gnuplot
Feb 18 '13 at 22:32
source share
4 answers

Sure (in gnuplot 4.4 +):

 plot for [i=1:1000] 'data'.i.'.txt' using 1:2 title 'Flow '.i 

Variable i can be interpreted as a variable or string, so you can do something like

 plot for [i=1:1000] 'data'.i.'.txt' using 1:($2+i) title 'Flow '.i 

if you want the lines to move apart.

Type help iteration at the gnuplot command line for more information.

Also, don't forget to see @DarioP's answer about do for syntax; which gives you something closer to the traditional for loop.

+80
Feb 18 '13 at 23:18
source share

See also the do { ... } command, since gnuplot 4.6 is as strong as possible:

 do for [t=0:50] { outfile = sprintf('animation/bessel%03.0f.png',t) set output outfile splot u*sin(v),u*cos(v),bessel(u,t/50.0) w pm3d ls 1 } 

http://www.gnuplotting.org/gnuplot-4-6-do/

+70
Jul 26 '13 at 14:53
source share

I have a script all.p

 set ... ... list=system('ls -1B *.dat') plot for [file in list] file wlu 1:2 t file 

Here, the last two lines are literal, not heuristic. Then i ran

 $ gnuplot -p all.p 

Change *.dat to the type of file you have, or add file types.

Next step: add this line to ~ / .bashrc

 alias p='gnuplot -p ~/./all.p' 

and put your all.p file in your home directory and voila. You can display all the files in any directory by typing p and typing.

EDIT I changed the command because it did not work. Previously, it contained list(i)=word(system(ls -1B *.dat),i) .

+8
Jan 30 '15 at 15:32
source share

I wanted to use wildcards to create multiple files, often placed in different directories, when working in any directory. The solution I found was to create the following function in ~/.bashrc

 plo () { local arg="wl" local str="set term wxt size 900,500 title 'wild plotting' set format y '%g' set logs plot" while [ $# -gt 0 ] do str="$str '$1' $arg," shift done echo "$str" | gnuplot -persist } 

and use it, for example. for example plo *.dat ../../dir2/*.out , display all .dat files in the current directory and all .out files in the directory that is at a higher level and is called dir2 .

+1
Jan 21 '16 at 1:24
source share



All Articles