使用 gnuplot 创建数据文件

RMS*_*RMS 2 terminal plot ascii gnuplot

大家好,我目前正在使用 gnuplot。
我有这个 .csv 文件,我一直用它来绘制一些公式
(例如plot "filename.csv" u 0:day($0) = $0)。阴谋成功了;但是,我想知道 gnuplot 中是否有一种方法可以将公式的输出也保存为数据文件。

the*_*ozh 6

请检查手册或在 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)