14
votes

I have some data.

#Time  Distance
 1   3
 2   5
 4   9
 8  11
12  17
14  20
16  34
20  40

I want to plot the cumulative distance wrt time in gnuplot ... (it should be easy) but I do not know how.

x

6
Hmm ... the columns were HTMLized away ... okay ... please take each row pair as pair of columns ... x - Xofo
Okay someone fixed it ... aah the joy of being an SO noob. - Xofo
@Xofo, just add 4 spaces before each line and the characters are even spaced. There are more solutions, but this works ;-). And welcome to SO. - Toon Krijthe

6 Answers

28
votes

For anyone still looking for this sort of thing, If your gnuplot version is 4.4 or newer, you can do the following:

a=0
#gnuplot 4.4+ functions are now defined as:  
#func(variable1,variable2...)=(statement1,statement2,...,return value)
cumulative_sum(x)=(a=a+x,a)
plot "test.dat" using 1:(cumulative_sum($2))
15
votes

If your data is in the file datafile, you can do a cumulative plot like so:

$ gnuplot
gnuplot> plot "datafile" smooth cumulative
6
votes

Assuming your data is in a file "test.txt", how about:

plot "<awk '{i=i+$2; print $1,i}' test.txt" with lines
6
votes

The same can be achieved by the "cumulative" variant of the "smooth" option (just enter help smooth in gnuplot).

1
votes

I have a little experience with Gnuplot and I just pored over the documentation some. Unfortunately, I wasn't able to find a solution for generating a cumulative sum as you're plotting.

I think what you'll need to do is to massage your data with another program before letting Gnuplot at it. awk is one program that comes to mind, it's practically built for fiddling with columnar data. You can integrate this process into the plotting process by following these instructions.

1
votes

This isn't what the question is looking for but "gnuplot cumulative distribution function" always leads me here and none of the answers are quite right unless you already have the data gnuplot expects.

But suppose you wanted a cumulative distribution function of just distances and only have the raw distances. There is a little trick with which gnuplot can calculate the y values:

test.dat:

#Time  Distance
 1   3
 2   5
 4   9
 8  11
12  17
14  20
16  34
20  40

plot.gpi:

datafile = test.dat
stats datafile
plot datafile using 2:(1./STATS_records) smooth cumulative title "distance"

enter image description here

It is not as flexible as having all the data, though.