Can gnuplot calculate and plot delta between consecutive data points

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

0 1 4 9 16 25 

Can gnuplot draw the dots along with the differences between the dots, 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 of interest, and I would like to avoid preprocessing to add delta if possible.

+7
source share
4 answers

How about using awk?

 plot "< awk '{print $1,$1-prev; prev=$1}' <datafilename>" 
+4
source

The dtop solution does not work for me, but it 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' 

An example data file named 'data.dat':

 0 1 4 9 16 25 
+9
source

Here's how to do it without preprocessing:

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' 

Example data file 'runtime_delta.dat':

 Data 0 1 4 9 16 25 
+6
source

Below is the version that uses arrays from Gnuplot 5.1. Using arrays allows you to calculate multiple differences in a single instance of Gnuplot.

 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 
0
source

All Articles