Search code examples
gnuplotaveragedata-processing

How do you sum every nth column of data in gnuplot?


I would like to take an average over several columns of a data set in Gnuplot. The problem is that I want to average every other column (starting from the second column of my dataset). I was thinking of using every somehow but I still don't really understand when and where to use every. To help visualise my question: my data looks something like this:

x  y1  z1  y2  z2
2  0.6  0  0.6  0
1  0.7  0  0.7  1
1  0.8  2  0.8  1
1  0.9  0  0.9  0

and I would like to average y1 and y2 and plot the result by doing something like:

stats filename nooutput
plot filename u 1:sum[col = every :2::2::STATS_columns] / ((STATS_columns-1)/2)

Not sure if this is anywhere close to doable though. Also, it would be nice to have a way of finding the number of columns used without any apriori knowledge of what the data looks like. In the example I have used my knowledge of the data to know that the average is over ((STATS_columns-1)/2) number of points.

Thank you for your response


Solution

  • From your code I assume you want to average y1 and y2 for each row and then plot it versus x (column 1). Since you have several identical x values, there would be another average, namely an average over the columns and over all identical x values. I modified your data to better illustrate the difference. I guess you were asking fot the red circles. The blue triangles are basically the average of the average, i.e. the average of the red points. Check help summation and help smooth. sum has no step size with the index. From gnuplot help:

    sum [<var> = <start> : <end>] <expression>
    

    Code:

    ### average over columns and smooth
    reset session
    
    $Data <<EOD
    #x  y1  z1  y2  z2
    1  2.0  0  4.0  0
    1  2.2  0  4.2  1
    1  2.9  2  4.9  1
    2  2.1  0  4.1  0
    2  2.3  0  4.3  0
    2  2.8  0  4.8  0
    3  2.2  0  4.2  0
    3  2.3  0  4.3  0
    3  2.7  0  4.7  0
    EOD
    
    stats $Data nooutput
    set offsets 0.5,0.5,0.5,0.5
    
    Count = (STATS_columns-1)/2
    plot $Data u 1:((sum[i=1:Count] column(i*2))/Count) w p pt 7 lc rgb "red" ti "average over y1,y2 columns for each row",\
         $Data u 1:((sum[i=1:Count] column(i*2))/Count) smooth unique w p pt 9 lc rgb "blue" ti "average over y1,y2 for each x"
    ### end of code
    

    Result:

    enter image description here