1
votes

I have a text file of the position of particles along the x axis which changes after every collision. Example data.

0   7.5 10  30  30  40  
0   9.375   10  32.5    40  40  
0   10  10  33.3333 36.6667 40  
0   10.25   10.75   34  34  40  
0   11.0938 13.2812 28.75   40  40  

I am currently trying to plot the data using gnu plot. What I want it to do is have these points along the x axis but instead of plotting the whole file at once I would like gnu plot to plot one line at a time . Furthermore, so the data is identifiable I am trying to plot the points as large markers instead of points. I am struggling to do this and any help would be appreciated.

2

2 Answers

2
votes

Firstly, convert the rows to columns using AWK

awk '{for(i=1;i<=NF;i++)a[NR,i]=$i}END{for(i=1;i<=NF;i++){for(j=1;j<=NR;j++)printf a[j,i]"\t";printf "\n"}}' original.dat > particle.dat
    #suppose that your input data is original.dat and the output data is particle.dat

The converted data are:

0   0   0   0   0   
7.5 9.375   10  10.25   11.0938 
10  10  10  10.75   13.2812 
30  32.5    33.3333 34  28.75   
30  40  36.6667 34  40  
40  40  40  40  40

Then, plot your data with the following code in gnuplot:

set border 1
    #`set border 1` means only showing the bottom border of the plot. see `help border` for more information
set xtics nomirror
    #only show the bottom tics on the x axis and suppress the upper tics of the x axis
unset ytics
    #suppress the tics on the y axis
set key outside
    #set the legend out side the plot
plot "particle.dat" using 1:(1) with points pointtype 7 pointsize 3 title "particle 1", "" u 2:(2) w p pt 7 ps 3 t "particle 2", "" u 3:(3) w p pt 7 ps 3 t "particle 3", "" u 4:(4) w p pt 7 ps 3 t "particle 4", "" u 5:(5) w p pt 7 ps 3 t "particle 5"
    #`using 1:(1)` means use the first column as X and a constant number of 1 as Y. see `help using` for more details
    #`u` is short for `using`and `w p pt 7 ps 3` is short for `with points pointtype 7 pointsize 3.

The output of the plot is enter image description here

1
votes

I don't think that you have to transpose the data using awk, as each row already contains the data of a single particle.

So, based on the code from DragonHu, I have this: enter image description here

To generate this plot, I also added lines to connect the points. Also, I used the special column number 0 which just gives the line number in the datafile, starting at 0.

Another trick: Using backslash \, you can split a command to multiple lines. Here is the plot command I used:

plot "particle.dat" using 1:0 with points linetype 1 pointtype 7 pointsize 3 title "particle 1",\
 "" u 1:0 notitle w l lt 1,\
 "" u 2:0 w p lt 2 pt 7 ps 3 t "particle 2", \
 "" u 2:0 notitle  w l lt 2,\
 "" u 3:0 w p lt 3 pt 7 ps 3 t "particle 3", \
 "" u 3:0 notitle  w l lt 3,\
 "" u 4:0 w p lt 4 pt 7 ps 3 t "particle 4", \
 "" u 4:0 notitle  w l lt 4,\
 "" u 5:0 w p lt 5 pt 7 ps 3 t "particle 5",\
 "" u 5:0 notitle  w l lt 5

Still, this is not yet the answer, as the question is to plot one set of points at a time. This can be achieved with the following code. It generates five single plots which I dumped into an animated gif figure:

 set key center

 set yrange[0:1]
 set xrange[0:40]

 set terminal gif size 600, 200 animate delay 100

 set output "animated.gif"
 do for [n=0:4] {
     set title sprintf("Lineno. %d", n)

     plot "particle.dat" every ::n::n  using 1:(0) with points pointtype 7 pointsize 3 title "particle 1",\
     "" every ::n::n  u 2:(0) w p pt 7 ps 3 t "particle 2", \
     "" every ::n::n  u 3:(0) w p pt 7 ps 3 t "particle 3", \
     "" every ::n::n  u 4:(0) w p pt 7 ps 3 t "particle 4", \
     "" every ::n::n  u 5:(0) w p pt 7 ps 3 t "particle 5",\


 }
 unset output

If single images should be created, it is possible via

 set terminal ongcairo 
 do for [n=0:4] {
     set title sprintf("Lineno. %d", n)
     set output sprintf("PictureNumber_%d",n)

     plot ...
     unset output


 }

enter image description here