Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can gnuplot compute and plot the delta between consecutive data points

Tags:

gnuplot

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.

like image 917
Greg Dunn Avatar asked Aug 30 '11 21:08

Greg Dunn


3 Answers

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
like image 139
BorisHajduk Avatar answered Nov 11 '22 11:11

BorisHajduk


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
like image 25
dtop Avatar answered Nov 11 '22 13:11

dtop


How about using awk?

plot "< awk '{print $1,$1-prev; prev=$1}' <datafilename>"
like image 42
Sunhwan Jo Avatar answered Nov 11 '22 11:11

Sunhwan Jo