Search code examples
loopsgnuplot

How to loop through columns in file and manipulate them in gnuplot


I want to loop through columns in a file and plot them against another column in a file. I am able to do this fine. However, when I try to manipulate the data of iterable column (i.e. shift it up or down) in a manner I would typically do for a normal column entity I get an error.

I have the following example data

1 5 6 7 2 3
2 5 2 1 6 5
3 5 2 3 6 2

and I would like to plot it with the loop function

plot for [col=2:5] 'example.txt' u 1:col

this works fine.

However, now I want to shift the the data up by 1.5. So I try

plot for [col=2:5] 'example.txt' u 1:($col+1.5)

and get the following error

gnuplot> plot for [col=2:5] 'so_data.txt' u 1:($col+1.5)
                                                ^
         Column number or datablock line expected

I am unsure how to get the desired outcome


Solution

  • You need:

    plot for [col=2:5] 'example.txt' u 1:(column(col)+1.5)  
    

    see:

    column The column(x) function may be used only in the using specifier of a plot, splot, fit, or stats command. It evaluates to the numerical value of the content of column x. If the column is expected to hold a string, use instead stringcolumn(x) or timecolumn(x, "timeformat"). See plot datafile using, stringcolumn, timecolumn.

    in help