2

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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))

3 Comments

You can do this sort of thing if you want to with only gnuplot (see my solution), but it quickly becomes cumbersome if you want the i-2, or i-3 element. And i+1 and i+2 elements cannot be retrieved (but that's not too much of a problem -- just shift your indexing :).
@mgilson - didn't think about such a solution :) the only problem I see is that you can't do a replot without reseting firstval
Definitely true -- Although I don't really find myself replotting very often these days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.