0

I have several files which have the same name, data, in different folders named folder1, folder2, folder3, I want to plot columns 1 and 2 of all files in the different folders at once. In other words, from the root where folder1..3 are located I don't want to use:

p 'folder1/data' u 1:2, 'folder1/data' u 1:3

and

p folder2/data' u 1:2, 'folder2/data' u 1:3

and again for folder3 and the 28 other folders, I have in the real case. Are there a for command or some related command in Gnuplot that I can, at once, plot all data files from the different folders, preferably in different windows?

user
  • 1
  • 1
  • 2

2 Answers2

1

Take a look at help for to learn about loops in gnuplot. It seems like

plot for [i=1:3] "folder".i."/data" u 1:2, for [i=1:3] "folder".i."/data" u 1:3

might be all you need.

Whether or not you can automatically create plots for different files in different windows probably depends on your chosen output terminal. For x11 you could probably do something like

do for [i=1:3] {
    set term x11 i
    plot "folder".i."/data" u 1:2, "" u 1:3
}

(not tested).

user8153
  • 4,049
  • 1
  • 9
  • 18
0

gnuplot can handle as many files as you like in a single command.

Simply issue your plot command separating each file and column specification with a comma (as you have begun doing above)

plot \
"folder1/data" using 1:2,
"folder1/data" using 1:3,
"folder2/data" using 1:2,
"folder2/data" using 1:3,
"folder3/data" using 1:2,
"folder3/data" using 1:3

Generally each plot command will produce output in a separate window (it depends on how gnuplot is called -- whether in a separate process or not).

When called from a shell script, you can spawn separate subshells to produce output in separate windows, or within a C-program, you can fork separate processes for the same purpose.

You can also use multiplot, see Multiplot – placing graphs next to each other « Gnuplotting

You may also find parts of you question answered here gnuplot : plotting data from multiple input files in a single graph


edit Based on Comment

OK, now that I understand you want to automate building a plot file from a list of directories named folderXX (where XX can be anything) below the current directory, and in each folder there will be a file named data where you want to plot the first against each of the 2nd and 3rd columns, then you can build a temporary plot file (say tmp.plt) by looping over the list of folderXX (you can adjust the globbing to your needs) and outputting the plot commands as you go using simple redirection.

For example, something like the following:

#!/bin/bash

## truncate tmp.plt and set line style
echo -e "set style data lines\nplot \\" > tmp.plt

cnt=0   ## flag for adding ',' line ending

## loop over each file
for i in folder*/data; do
    if ((cnt == 0)); then       ## check flag (skips first iteration)
        cnt=1                   ## set flag to write ending comma
    else
        printf ",\n" >> tmp.plt             ## write comma
    fi
    printf "\"$i\" using 1:2,\n" >> tmp.plt ## write using 1:2
    printf "\"$i\" using 1:3" >> tmp.plt    ## write using 1:3 (no ending)
done
echo "" >> tmp.plt              ## write final newline

(note: echo and printf are alternated to get the desired escape behavior for the line-continuations)

When run within a directory containing folder1, folder2, folder3, folder4, each containing a data file, e.g.

$ tree
.
├── folder1
│   └── data
├── folder2
│   └── data
├── folder3
│   └── data
├── folder4
│   └── data

it will result in a tmp.plt plotfile that can then be called with gnuplot -p tmp.plt, e.g.

$ cat tmp.plt
set style data lines
plot \
"folder1/data" using 1:2,
"folder1/data" using 1:3,
"folder2/data" using 1:2,
"folder2/data" using 1:3,
"folder3/data" using 1:2,
"folder3/data" using 1:3,
"folder4/data" using 1:2,
"folder4/data" using 1:3

This should be closer to what you were looking for based on your comment.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • I would like to avoid writing that 56 times (28 folders x 2 columns each), that's why I was wondering if some sort of loop, as the name of the files are the same, and the directories names are numbered. – user Jul 06 '18 at 00:52
  • Oh -- now it is clear... you can just build the gnuplot command dynamically or you can write your plot command to a file and then execute that. Hold on and I'll look at putting together an example. **How are your folders ordered?** and **Do all folders under a directory contain `data` files?** – David C. Rankin Jul 06 '18 at 01:13