Gnuplot Script for average over multiple files

gnuplot, graph, visualization

Solution

You cannot do it all in gnuplot. Gnuplot is limited to processing columns from one file at a time. You need some other utility to preprocess your data. Assuming the data is in the format you demonstrated (with spaces instead of semicolons), this script will take the average of the second, third and fourth columns and spit out a file with the first and fifth columns the same, and the middles averaged. Run this bash script in a directory where there are only the `.txt` files you want to process.

#!/bin/bash

sum=$(ls -l *.txt | wc -l)
paste -d" " *.txt | nawk -v s="$sum" '{
    for(i=0;i<=s-1;i++)
    {
        t1 = 2+(i*5)
        temp1 = temp1 + $t1
        t2 = 3+(i*5)
        temp2 = temp2 + $t2
        t3 = 4+(i*5)
        temp3 = temp3 + $t3
    }
    print $1" "temp1/s" "temp2/s" "temp3/s" "$5
    temp1=0
    temp2=0
    temp3=0
}'

From inside gnuplot, you can run the script like this:

!myscript.sh > output.out
plot 'output.out' u 1:2 # orwhatever

or like this:

plot '<myscript.sh' u 1:2

(code inspired by what I found here.)

Problem

I have series of measurement in several files. Every file is looking like this: ``` 1 151.973938 144.745789 152.21991 17:57:14 2 151.995697 144.755737 152.21991 17:57:14 3 152.015747 144.765076 152.21991 17:57:14 . . . ``` I'm looking for a possibility to compute the average of the same field in several files. At the end of the process I would like to have a graph of the average measurement. Is that possible with gnuplot? I wasn't able to find a fitting option in gnuplot by myself. If not, which different way to achieve that would you recommend? Best regards, Juuro

Original source