Linux

Trucos En Gnuplot


GNUplot y Xfig

5 de Octubre, 2016

Export a plot from GNUplot to Xfig

Save the plot int the terminal "fig":

 set terminal fig
 set output "myfigure.fig"
 plot x, x**2

Open the file with "myfigure.fig" using Xfig.

FOR LOOP

June 5, 2016

PLOT MULTIPLE FILES

If we have many files, let's say data1.txt, data2.txt, ..., data1000.txt, and we want to plot them all together, we could do it manually writing each of them:

 plot "data1.txt" using 1:2 title "Data 1", \
      "data2.txt" using 1:2 title "Data 2", \
      .
      .
      .
     "data1000.txt"  using 1:2 title "Data 1000"

However, we could use the for loop to do it much easily:

 plot for [j=0:1000:100] sprintf('data%i.dat',j) u 1:4 w l t sprintf('Data %i',j)

In this example, j takes values from 0 to 1000, each 100. Therefore, we obtain 10 curves in a single plot. We can also look for the files using the command ls from the terminal through the system command:

plot for [j in system('ls HHe0*/E.dat')] sprintf('%s',j) w lp  t  sprintf('%s',j)

VASP users may like this:

plot for [j in "08 12 16 24 32"] sprintf("<awk '/LOOP\\+/{print}' Simulation-%s/OUTCAR",j)  u 0:7 w lp  t sprintf('Simulation-%s.dat',j)

GENERATE PLOTS FROM A FILE WITH MULTIPLE TABLES

Let's consider a file written in the following way

 # x   f(x)
  0.1  0.0998334
  0.2  0.198669
  0.3  0.29552
  # x   f(x)
  0.1  0.891207
  0.2  0.932039
  0.3  0.963558
  .
  .
  .
  # x   f(x)
  0.1  -0.625071
  0.2  -0.699875
  0.3  -0.767686

If each block corresponds to a function f(x) at a given step, in principle we would have to divide this files in many small files, and then plot all of them using

 plot "data1.txt" using 1:2 title "Block 1", \
      "data2.txt" using 1:2 title "Block 2", \
      .
      .
      .
     "data1000.txt"  using 1:2 title "Block 6"

To avoid doing this, and be able to do everything at once from the original file, we will extract each block using awk from gnuplot though a function. Then, we will do a for loop that calls this function with different arguments:

 filename(b,j) = sprintf("<awk 'NR>%i*%i && NR<=%i*(%i+1){print}' temp.dat", b,j,b,j)
 plot for[j=0:900:300] filename(4,j) u 1:2 w lp t sprintf("Block %i",j)

The variable b of the function filename represents the size of each block (amount of lines of each table) in the file. In this case, each table is a block with 4 lines. The variable j represents the number of the block (step). Note that the for loop can be initiated with any value (j=0), end with any value (j=900), and do it every any amount of steps (300 in this case).