8
votes

For instance, given the following data file (x^2 for this example):

0
1
4
9
16
25

Can gnuplot plot the points along with the differences between the points as if it were:

0 0
1 1   # ( 1 - 0 = 1)
4 3   # ( 4 - 1 = 3)
9 5   # ( 9 - 4 = 5)
16 7  # (16 - 9 = 7)
25 9  # (25 -16 = 9)

The actual file has more than just the column I'm interested in and I would like to avoid pre-processing in order to add the deltas, if possible.

4

4 Answers

4
votes

How about using awk?

plot "< awk '{print $1,$1-prev; prev=$1}' <datafilename>"
16
votes

dtop's solution didn't work for me, but this works and is purely gnuplot (not calling awk):

delta_v(x) = ( vD = x - old_v, old_v = x, vD)
old_v = NaN
set title "Compute Deltas"
set style data lines
plot 'data.dat' using 0:($1), '' using 0:(delta_v($1)) title 'Delta'

Sample data file named 'data.dat':

0
1
4
9
16
25
7
votes

Here's how to do this without pre-processing:

Script for gnuplot:

# runtime_delta.dem script
# run with 
#     gnuplot> load 'runtime_delta.dem'
#
reset

delta_v(x) = ( vD = x - old_v, old_v = x, vD)
old_v = NaN

set title "Compute Deltas"
set style data lines

plot 'runtime_delta.dat' using 0:(column('Data')), '' using 0:(delta_v(column('Data'))) title 'Delta' 

Sample data file 'runtime_delta.dat':

Data
0
1
4
9
16
25
0
votes

Below is a version that uses arrays from Gnuplot 5.1. Using arrays allows multiple diffs to be calculated in single Gnuplot instance.

array Z[128]
do for [i=1:128] { Z[i] = NaN }
diff(i, x) = (y = x - Z[i], Z[i] = x, y)

i is the instance index that needs to be incremented for each use. For example

plot "file1.csv" using 1:(diff(1,$2)) using line, \
     "file2.csv" using 1:(diff(2,$2)) using line