Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot all files in a directory simultanously with gnuplot?

Tags:

gnuplot

I want to do something similar to this question: gnuplot : plotting data from multiple input files in a single graph.

I want to plot simultaneously all the files in a directory, without having to explicitly write their names. The column numbers are the same for all the files. What can I do?

Doing plot for [file in *] file u 3:2 doesn't work.

Also, I don't want each file to have a different legend. All points from all files should be treated the same, as if they all came from a single file.

like image 357
becko Avatar asked Apr 30 '15 13:04

becko


3 Answers

You could try something like:

a=system('a=`tempfile`;cat *.dat > $a;echo "$a"')
plot a u 3:2

This uses the command line tempfile command to create a safe, unique, and disposable temporary file. It mashes all of the data files into this file. It then echoes the file's name so gnuplot can retrieve it. Gnuplot then plots things.

Worried about header lines? Try this:

a=system('a=`tempfile`;cat *.dat | grep "^\s*[0-9]" > $a;echo "$a"')

The regular expression ^\s*[0-9] will match all lines which begin with any amount of whitespace followed by a number.

like image 67
Richard Avatar answered Oct 19 '22 15:10

Richard


As an alternative to Jonatan's answer, I would go with

FILES = system("ls -1 *.dat")
plot for [data in FILES] data u 1:2 w p pt 1 lt rgb 'black' notitle

or

plot '<(cat *.dat)' u 3:2 title 'your data'

The first option gives you more flexibility if you want to label each curve. For example, if you have several files with names data_1.dat, data_2.dat, etc., which will be labeled as 1, 2, etc., then:

FILES = system("ls -1 data_*.dat")
LABEL = system("ls -1 data_*.dat | sed -e 's/data_//' -e 's/.dat//'")

plot for [i=1:words(FILES)] word(FILES,i) u 3:2 title word(LABEL,i) noenhanced
like image 31
vagoberto Avatar answered Oct 19 '22 15:10

vagoberto


I like to be able too choose the files to plot with wildcards, so if you like that you can do as follows, though there are many ways. Create the following script.

script.sh:

gnuplot -p << eof
set term wxt size 1200,900 title 'plots'
set logs
set xlabel 'energy'
plot for [ file in "$@" ] file w l
eof

do chmod u+x script.sh

Run like ./script.sh dir/* *.dat

If you need it often make an alias for it and put it in some reasonable place:) Cheers /J

like image 2
Jonatan Öström Avatar answered Oct 19 '22 15:10

Jonatan Öström