andrepd
andrepd

Reputation: 607

How to plot data from different blocks with lines in Gnuplot?

I have a data file with blocks of x/y values. Each block contains 16 lines with x/y pairs and each block represents those positions in a different time. http://pastebin.com/0teRrfRU

I want to plot the trajectory of a specific particle. To do that, I've written plot 'pos.dat' u 2:3 every ::n:0:n:i, where n is the n-th particle and i is the time up to which I want the trajectory plotted (I can then loop over the i to generate an animation).

This runs fine, but when I add w lines nothing gets plotted, and I don't understand why. Is there a way to plot this with lines? The only alternative I see is writing a script to parse the data file and generate a new one with only the values I want (effectively acting as every), but I don't want to do that if I can do it in Gnuplot.

Upvotes: 3

Views: 609

Answers (1)

theozh
theozh

Reputation: 25938

After a closer look to your data, your case has some speciality. Like in Plotting same line number of several blocks data with gnuplot you can plot the file into a table via with table which will remove the empty lines and hence lines will be connected.

However, some of your particles disappear on one side and re-appear on the opposite side. If you plot this with lines you will get a line through the whole graph which is certainly undesired. You can workaround this if you introduce a function Break() which returns NaN if the difference of two successive x- or y-values are larger than 90% (to be on the safe side) of the x- or y-range , respectively. The effect of NaN is that the line will interrupted.

Code: (works with gnuplot>=5.0.0 version at the time of OP's question)

### plotting trajectories
reset session

set term gif animate delay 3 size 400,400
set output "SO30744875.gif"
set size square

FILE = 'SO30744875.dat'

set key noautotitle
stats FILE u (N=column(-1),M=column(1),$2):3 nooutput
xrange = STATS_max_x-STATS_min_x
yrange = STATS_max_y-STATS_min_y

set table $Data
    plot FILE u 1:2:3 w table
unset table

Break(col1,col2) = (x0=x1,x1=column(col1), y0=y1,y1=column(col2), \
                   abs(x1-x0)<0.9*xrange && abs(y1-y0)<0.9*yrange ? column(col2) : NaN)

do for [i=0:N] {
    plot for [j=1:16] x1=y1=NaN $Data u 2:(Break(2,3))every M::j-1::(i+1)*M w l, \
                      FILE u 2:3   every :::i::i w p pt 7, \
                      FILE u 2:3:1 every :::i::i w labels offset 0.7,0.7
}
set output
### end of code

Result:

enter image description here

Upvotes: 1

Related Questions