这个问题 (gnuplot store one number from data file into variable) 在第一个答案中对我有一些提示。
在我的例子中,我有一个包含抛物线参数的文件。我已将参数保存在 gnuplot 变量中。然后我绘制包含每个时间步的参数变量的函数。
#!/usr/bin/gnuplot
datafile = "parabola.txt"
set terminal pngcairo size 1000,500
set xrange [-100:100]
set yrange [-100:100]
titletext(timepar, apar, cpar) = sprintf("In timestep %d we have parameter a = %f, parameter c = %f", timepar, apar, cpar)
do for [step=1:400] {
set output sprintf("parabola%04d.png", step)
# read parameters from file, where the first line is the header, thus the +1
a=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $1}' " . datafile)
c=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $2}' " . datafile)
# convert parameters to numeric format
a=a+0.
c=c+0.
set title titletext(step, a, c)
plot c+a*x**2
}
这给出了一系列名为 parabola0001.png 的 png 文件,
抛物线0002.png,
抛物线0003.png,
…,每个都显示一个抛物线,其参数从名为parabola.txt 的文件中读取。标题包含给定时间步的参数。
要了解 gnuplot system() 函数,您必须知道:
- gnuplot 不解析双引号内的内容
- 点用于连接 gnuplot 中的字符串
- 必须对 awk
printf 命令的双引号进行转义,以便对 gnuplot 解析器隐藏它们
要测试这个 gnuplot 脚本,请将其保存到具有任意名称的文件中,例如parabolaplot.gplot 并使其可执行 (chmad a+x parabolaplot.gplot)。 parabola.txt 文件可以用
创建
awk 'BEGIN {for (i=1; i<=1000; i++) printf "%f\t%f\n", i/200, i/100}' > parabola.txt