Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot of difference between current record and previous record of two different columns in the same data file in gnuplot

I have 3 columns of data in a text file. First column is time. Second and Third columns are my variable parameters. My intention is to plot the difference in the current value of data from 3rd column and the previous data value from the 2nd column against 1st column current value.

like image 721
Soumajit Avatar asked Oct 28 '25 10:10

Soumajit


1 Answers

That is a bit tricky since gnuplot usually has access only to the values of the current row. However, you can keep a limited number of previous values in memory with some trickery inside the using statement.

Unfortunately you don't give any example data, so here is my test data file test.dat (without times, you'll need to adapt that by yourself):

0 1 5
1 12 7
2 1 6
3 5 5

The following script plots the difference between the current value in column 3 and the previous value in column 2:

back2 = back1 = 0
shift(x) = (back2 = back1, back1 = x)
plot 'test.dat' using 0:(shift($2), $0 < 1 ? 1/0 : $3 - back2) w lp pt 7 ps 2

What happens here is basically the following:

Inside the using statement you can separate several statements like assignments with commas. Only the last expression in one column is used as actual data value.

The shift function the second column first assigns the value of the previous row to variable back2 and then the current value to variable back1. So, calling shift($2) saves the value in column 2 of the previous row to variable back2. Then you can do the actual computation as $3 - back2. The condition $0 < 1 discards the first row for which you don't have a previous value.

The result is:

enter image description here

like image 116
Christoph Avatar answered Oct 30 '25 08:10

Christoph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!