13

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.

Community
  • 1
  • 1
a06e
  • 18,594
  • 33
  • 93
  • 169
  • See e.g. http://stackoverflow.com/a/26682241/2604213 – Christoph Apr 30 '15 at 14:06
  • @Christoph Thanks +1, that almost solves my problem. I edited the question, though, since I don't want each file to have a distinct legend. – a06e Apr 30 '15 at 14:33
  • Which plotting style do you want, do you need a legend at all and which OS do you work on? – Christoph Apr 30 '15 at 15:02
  • 1
    @Christoph I am on Ubuntu 14.04.2 64 bits. Since I have only one dataset (the data from all the files combined), I don't need a legend. – a06e Apr 30 '15 at 15:20

4 Answers4

10

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
vagoberto
  • 2,372
  • 20
  • 30
8

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.

Richard
  • 56,349
  • 34
  • 180
  • 251
2

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

Jonatan Öström
  • 2,428
  • 1
  • 16
  • 27
0

Try the following command:

gnuplot -e 'plot for [file in system("find . -depth 1 -type f -print")] file u 3:2'

Note: Add -p to keep the plot window.

kenorb
  • 155,785
  • 88
  • 678
  • 743