Search code examples
gnuplot

gnuplot: plotting the average of columns with increment


I have a data with 109 columns and around 3000 rows. I would like to plot the average of the x1 until x108 (ignoring the y and z), the data looks like:

time x1 y1 z1 x2 y2 z2 x3 y3 z4 ...x108 y108 z108

With just a few columns it works well, for example:

time x1 y1 z1 x2 y2 z2 x3 y3 z3

plot 'file.dat' u 1:(($2+$5+$8)/3) with lines ls 4

But the problem comes when the data is bigger, for instance 108 columns or more. I would not like to do it manually, because the data will get bigger later.

I have tried:

plot for [i=2:108:3] 'file.dat' u 1:(column(i)) with lines ls 4

But then I would get plots for each combination, which is not what I want. So, how could I plot just the average of x1 ... x108 (ignoring the y and z)?

Thanks.


Solution

  • The following should do what you're looking for. The example below averages z1, ..., z3. In your case the parameters for averaging x1, ..., x108 would be ColStart=2, ColStep=3, and ColCount=108.

    Also check help summation.

    Code:

    ### average over several columns
    reset session
    
    $Data <<EOD
    #n   x1   y1   z1   x2   y2   z2   x3   y3   z3
     1 1.11 1.21 1.31 2.11 2.21 2.31 3.11 3.21 3.31
     2 1.12 1.22 1.32 2.12 2.22 2.32 3.12 3.22 3.32
     3 1.13 1.23 1.33 2.13 2.23 2.33 3.13 3.23 3.33
     4 1.14 1.24 1.34 2.14 2.24 2.34 3.14 3.24 3.34
     5 1.15 1.25 1.35 2.15 2.25 2.35 3.15 3.25 3.35
     6 1.16 1.26 1.36 2.16 2.26 2.36 3.16 3.26 3.36
     7 1.17 1.27 1.37 2.17 2.27 2.37 3.17 3.27 3.37
     8 1.18 1.28 1.38 2.18 2.28 2.38 3.18 3.28 3.38
     9 1.19 1.29 1.39 2.19 2.29 2.39 3.19 3.29 3.39
    EOD
    
    ColStart = 4
    ColStep = 3   
    ColCount = 3
    
    plot $Data u 1:((sum[i=0:ColCount-1] column(i*ColStep+ColStart))/ColCount) w lp pt 7 notitle
    ### end of code
    

    Result:

    enter image description here