Reputation: 381
I have a data file with two columns: Xi
and Yi
. I'd like to plot Xi
vs. (Yi-1 - Yi)/Yi-1
for i>1
. Is is possible to do that in GNUPlot direclty?
Upvotes: 2
Views: 442
Reputation: 69092
To do this in gnuplot directly is tricky. The problem is that you have to use the (i-1)th element in your calculation, which gnuplot can't do automatically. It can do simple calculations, but only on the same row, for example something like
plot "datafile" using ($1):(($2-$1)/$2)
would be easy.
For what you neet to do I'd recommend octave, or you could prepare your data file using a spreadsheet application.
In octave, you could plot this like:
d = load("datafile")
plot(d(2:end, 1), d(1:end-1, 2)-d(2:end, 2)./d(1:end-1, 2))
Upvotes: 0
Reputation: 310287
Yes it is possible with gnuplot directly -- It's just not easy:
firstval = NaN
yi1(yi) = (returnval=firstval, firstval=yi, returnval)
plot "datafile" using 1:((yi1($2)-$2)/returnval)
You need to use inline functions. inline functions are of the form:
funcname(args,...) = (statement1,statement2,...,statementn, return_value)
Here I just created a function to hold the last value it was passed. Unforunately, this solution gets a little more ugly since I couldn't call yi1
twice in the using specification (the second time, I would get the wrong return value), so I had to reference the variable holding the return value directly. It's not pretty, but it works. You could probably "pretty" it up a little bit by passing $0
(the line number) and only updating when $0 changes, but it's probably not worth it for this hack.
Upvotes: 2