Using gnuplot to create data files

后端 未结 1 593
一向
一向 2021-01-16 03:47

Hey guys I am currently working with gnuplot.
I have this .csv file which I have been using to plot some formulas
(eg plot "filename.csv" u 0:da

相关标签:
1条回答
  • 2021-01-16 04:34

    Please check the manual or in the gnuplot console type help table.

    Code:

    ### save data as text
    reset session
    
    f(x) = x
    g(x) = x**2
    h(x) = x**3
    
    set xrange[-5:5]
    set samples 11
    
    plot f(x) w lp, g(x) w lp, h(x) w lp
    
    set table "myOutput.dat"
        plot '+' u 1:(f($1)):(g($1)):(h($1)) w table
    unset table
    
    ### end of code
    

    Edit:

    Actually, to be more flexible with data separators (e.g. comma or whatever) in the output file, you could change the plot ... w table command to something like the line below. However, I guess, gnuplot will always add a leading space " " and a trailing TAB \t for each line. But maybe this can also be changed.

    plot '+' u (sprintf("%g,%g,%g,%g",$1,f($1),g($1),h($1))) w table
    

    Result:

    And myOutput.dat:

     -5  -5  25  -125
     -4  -4  16  -64
     -3  -3  9   -27
     -2  -2  4   -8
     -1  -1  1   -1
     0   0   0   0
     1   1   1   1
     2   2   4   8
     3   3   9   27
     4   4   16  64
     5   5   25  125
    

    Addition: (creating data in a loop)

    With set print you are probably the most flexible, no leading space and trailing TAB. Check the manual or in gnuplot console type help set print.

    Code:

    ### save data as text, independent of range and samples
    reset session
    
    f(x) = x
    g(x) = x**2
    h(x) = x**3
    
    set print "myOutput.dat"
        do for [i=-5:5] {   
            # loop index only takes integers, multiply i with some factor if necessary
            print sprintf("%g,%g,%g,%g",i,f(i),g(i),h(i))
        }
    set print
    ### end of code
    
    0 讨论(0)
提交回复
热议问题