Bits from Matthieu Caneill

Turn x-y-z formatted data into a grid of z, for Gnuplot

Posted: October 15, 2015
Categories: hack, gnuplot, linux

If it happens that you want to create a surface in Gnuplot, and you have your data formatted this way:

x1 y1 value1
x1 y2 value1
x2 y1 value3
x2 y2 value4

You will have to turn it into such a grid:

value1 value2
value3 value4

in order for Gnuplot to plot it easily (that is, without you describing your data format and telling which fields to extract).

To do so, you can use a combination of cut and the xargs -L <N> echo trick. cut will extract the right column, the third one in our case, while xargs -L 2 will group lines by 2. Feeding this to echo will display our blocks and add a newline between them. Naturally, replace 2 by the width of the desired grid.

You should obtain something alike:

cat input-file | cut -d' ' -f3 | xargs -L 6 echo > output-file

And your output-file is ready to represent a Gnuplot surface.

My 2 cents! Hope that helps.