请检查手册或在 gnuplot 控制台中输入help table
。
代码:
### 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
Run Code Online (Sandbox Code Playgroud)
编辑:
实际上,为了更灵活地使用输出文件中的数据分隔符(例如逗号或其他),您可以将命令更改plot ... w table
为类似下面的行。然而,我猜想,gnuplot 总是会为每一行添加一个前导空格" "
和一个尾随制表符\t
。但也许这也可以改变。
plot '+' u (sprintf("%g,%g,%g,%g",$1,f($1),g($1),h($1))) w table
Run Code Online (Sandbox Code Playgroud)
结果:
和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
Run Code Online (Sandbox Code Playgroud)
添加:(循环创建数据)
你set print
可能是最灵活的,没有前导空格和尾随制表符。检查手册或在 gnuplot 控制台中输入help set print
。
代码:
### 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
Run Code Online (Sandbox Code Playgroud)