Loop structure inside gnuplot?

前端 未结 6 992
孤城傲影
孤城傲影 2020-11-27 10:40

Is there any way to iteratively retrieve data from multiple files and plot them on the same graph in gnuplot. Suppose I have files like data1.txt, data2.txt......data1000.tx

相关标签:
6条回答
  • 2020-11-27 10:59

    I wanted to use wildcards to plot multiple files often placed in different directories, while working from any directory. The solution i found was to create the following function in ~/.bashrc

    plo () {
    local arg="w l"
    local str="set term wxt size 900,500 title 'wild plotting'
    set format y '%g'
    set logs
    plot"
    while [ $# -gt 0 ]
            do str="$str '$1' $arg,"
            shift
    done
    echo "$str" | gnuplot -persist
    }
    

    and use it e.g. like plo *.dat ../../dir2/*.out, to plot all .dat files in the current directory and all .out files in a directory that happens to be a level up and is called dir2.

    0 讨论(0)
  • 2020-11-27 11:00

    I have the script all.p

    set ...
    ...
    list=system('ls -1B *.dat')
    plot for [file in list] file w l u 1:2 t file
    

    Here the two last rows are literal, not heuristic. Then i run

    $ gnuplot -p all.p
    

    Change *.dat to the file type you have, or add file types.

    Next step: Add to ~/.bashrc this line

    alias p='gnuplot -p ~/./all.p'
    

    and put your file all.p int your home directory and voila. You can plot all files in any directory by typing p and enter.

    EDIT I changed the command, because it didn't work. Previously it contained list(i)=word(system(ls -1B *.dat),i).

    0 讨论(0)
  • 2020-11-27 11:00

    Here is the alternative command:

    gnuplot -p -e 'plot for [file in system("find . -name \\*.txt -depth 1")] file using 1:2 title file with lines'
    
    0 讨论(0)
  • 2020-11-27 11:01

    There sure is (in gnuplot 4.4+):

    plot for [i=1:1000] 'data'.i.'.txt' using 1:2 title 'Flow '.i
    

    The variable i can be interpreted as a variable or a string, so you could do something like

    plot for [i=1:1000] 'data'.i.'.txt' using 1:($2+i) title 'Flow '.i
    

    if you want to have lines offset from each other.

    Type help iteration at the gnuplot command line for more info.

    Also be sure to see @DarioP's answer about the do for syntax; that gives you something closer to a traditional for loop.

    0 讨论(0)
  • Take a look also to the do { ... } command since gnuplot 4.6 as it is very powerful:

    do for [t=0:50] {
      outfile = sprintf('animation/bessel%03.0f.png',t)
      set output outfile
      splot u*sin(v),u*cos(v),bessel(u,t/50.0) w pm3d ls 1
    }
    

    http://www.gnuplotting.org/gnuplot-4-6-do/

    0 讨论(0)
  • 2020-11-27 11:08

    Use the following if you have discrete columns to plot in a graph

    do for [indx in "2 3 7 8"] {
      column = indx + 0
      plot ifile using 1:column ;  
    }
    
    0 讨论(0)
提交回复
热议问题