0
votes

I compute the iterated positions of multiple particles, so that my output file looks like :

x1(t=0) y1(t=0)
x2(t=0) y2(t=0)
...
xn(t=0) yn(t=0)

x1(t=1) y1(t=1)
...
xn(t=1) yn(t=1)

(a lot of blocks)

x1(t=p) y1(t=p)
...
xn(t=p) yn(t=p)

For example, the particle 1 is on each first line of a block, etc.

I need to plot the trajectory of each particle in a single plot, with points linked with lines. The problem I stumble upon is to link properly the points corresponding to the correct particle. I found some advice recommending to reformat the data but I have no idea how to handle it. It might be also possible to plot directly the trajectories with a plot command but once again I am low on solutions.

2

2 Answers

0
votes

You should be able to do it with a loop (in gnuplot >= 4.6) and the index option to the plot command:

p = (number of particles)
plot for [i=0:p] 'data.dat' index i with linespoints

The with linespoints option also sounds like what you want, which links the data points with lines.

0
votes

Unfortunately, there is no way to do this with your current datafile setup. You can make a plot which doesn't connect the points using the every (e) keyword:

plot for [i=0:NPOINTS-1] 'test.dat' e ::i::i w p

But, that's not very helpful really if you want the datasets connected, you need to "invert" your data. I'd use python because it's super easy:

#pythonscript.py
import sys  #allow us to get commandline arguments

#store data as
#[[x1(t=0) y1(t=0),x2(t=0) y2(t=0),x3(t=0) y3(t=0),...],
# [x1(t=1) y1(t=2),x2(t=2) y2(t=2),x3(t=2) y3(t=2),...],
#  ... 
# [x1(t=N) y1(t=N),x2(t=N) y2(t=N),x3(t=N) y3(t=N),...],
#]

with open(sys.argv[1]) as fin:
    data = []
    current = []
    data.append(current)
    for line in fin:
        line = line.rstrip()
        if line:
           current.append(line)
        else:
           current = [] 
           data.append(current)


#now transpose the data an write it out.  `zip(*data)` will give you:
#[(x1(t=0) y1(t=0),x1(t=1) y1(t=1),x1(t=2) y3(t=2),...),
# (x2(t=0) y2(t=0),x2(t=1) y2(t=1),x2(t=2) y2(t=2),...),
# ...
# (xN(t=0) yN(t=0),xN(t=1) yN(t=1),xN(t=2) yN(t=2),...),
#]
for lst in zip(*data):
    for dpoint in lst:
        print dpoint
    print

For me, given the input file (test.dat):

x1(t=0) y1(t=0)
x2(t=0) y2(t=0)
xn(t=0) yn(t=0)

x1(t=1) y1(t=1)
x2(t=1) y2(t=1)
xn(t=1) yn(t=1)

x1(t=p) y1(t=p)
x2(t=p) y2(t=p)
xn(t=p) yn(t=p)

running python pythonscript.test.dat gives:

x1(t=0) y1(t=0)
x1(t=1) y1(t=1)
x1(t=p) y1(t=p)

x2(t=0) y2(t=0)
x2(t=1) y2(t=1)
x2(t=p) y2(t=p)

xn(t=0) yn(t=0)
xn(t=1) yn(t=1)
xn(t=p) yn(t=p)

Now you can plot that using the solution by andyras:

plot for [i=0:NP] '< python pythonscript.py data.dat' index i w lp