Search code examples
gnuplot

how to use for loop gnuplot command


I use gnuplot command below to plot trajectories

plot -22.5 lw 3,22.5 lw 3, 'hydro_15/hydro_15.01/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'black','hydro_15/hydro_15.02/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'orange','hydro_15/hydro_15.03/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'violet','hydro_15/hydro_15.04/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'red','hydro_15/hydro_15.05/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'cyan','hydro_15/hydro_15.06/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'blue','hydro_15/hydro_15.07/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'green','hydro_15/hydro_15.08/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'magenta','hydro_15/hydro_15.09/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'brown','hydro_15/hydro_15.10/tracer_com.dat' u (($2)**2+($3)**2)**0.5:4 w l lc 'coral'

Can I make it shorter maybe by using for loop or something else. Sometime I have 40 files to plot which makes command very lengthy


Solution

  • Since you are using different colors by name, I assume you want to plot all datafiles into one graph (no different frames or animations, etc.)

    You could do something like this: (here just a loop of 9 files and colors, assuming your files have two digit numbers ranging from 01 to 09)

    Script:

    ### plotting files in a loop into one graph
    reset session
    
    myFile(i) = sprintf('hydro_15/hydro_15.%02d/tracer_com.dat',i)
    
    myColors = "black orange violet red cyan blue green magenta brown"
    myColor(i) = word(myColors,i)
    
    myCalc(col1,col2) = sqrt(column(col1)**2 + column(col2)**2)
    
    plot -22.5 lw 3, \
          22.5 lw 3, \
          for [i=1:9] myFile(i) u (myCalc(2,3)):4 w l lc myColor(i)
    ### end of script