Question

I'm trying to draw a heatmap via gnuplot. The problem is: how to accumulate data with gnuplot. Starting with one dataset:

0 0 0
0 1 1
1 0 2
1 1 3

that can be easily plot via

set view map
splot 'test.data' using 2:1:3 with image 

The problem is: there is not only one dataset, but many. See this example data:

0 0 0
0 1 1
1 0 2
1 1 3
0 0 3
0 1 2
1 0 1
1 1 20

It has repeating x/y-values. Is it possible to use gnuplot to sum up the third column (the "data-column" like displayed here:

0 0 0       0 0 3       0 0 3
0 1 1       0 1 2       0 1 3
1 0 2   +   1 0 1   =   1 0 3
1 1 3       1 1 20      1 1 23

My first idea was to use every like in plot 'test.data' using 2:1:3 every 4 with image. But this doesn't work. Does anyone have an idea how to do this?

For the interested ones: i want to plot a heatmap of my fitbit data: https://gist.github.com/senfi/c0d13a2c91fae13bc5f5 This file contains nine weeks of counted steps i made. the first column is the day of the week (sunday to saturday). The second column represents 5-minute-steps through the day starting at 0:00am. Plotting a single week looks nice, but plotting the sum/average of the last two years may look pretty awesome. Of course, i will post a picture, if we figure it out how to plot this. Feel free to use the steps-data.

Was it helpful?

Solution

This looks like a job for awk to me. awk can be called from within gnuplot like this:

sp '<awk ''{a[$1,$2]+=$3}END{for(i in a){split(i,s,SUBSEP);print s[2],s[1],a[i]}}'' test.data' w image

The awk script accumulates the value of the third column into the array a. The key for each value is the string [$1 SUBSEP $2] (equivalent to [$1,$2]). $N is the value of column N. SUBSEP is a built in variable whose value we don't need to worry about, we just refer to it again later.

When the whole file has been read (the END block), split is used to recover the first two columns by breaking up the array keys. The two parts of the key are printed, followed by the accumulated value. I rearranged the column order in awk as well (print s[2],s[1],a[i]) so that back in gnuplot, using 2:1:3 is no longer needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top