Using gnuplot to create data files

假如想象 提交于 2020-07-30 03:44:48

问题


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:day($0) = $0 ). The plots worked out; however, I was wondering if there was a way within gnuplot to save the output of my formulas as a data file too.


回答1:


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


来源:https://stackoverflow.com/questions/62981355/using-gnuplot-to-create-data-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!