将数据传递给 gnuplot

Pass data to gnuplot

我有这个 bash 脚本:

#!/bin/bash

foo=42
./test.gp

这是 gnuplot 脚本 (test.gp):

#!/usr/bin/gnuplot

set grid
set title "This is the title (`echo $foo`)"

set terminal png large
set output "/tmp/test.png"
set samples 50, 50
plot [-10:10] sin(x)

我相信这应该将标题显示为 This is the title (42)。但事实并非如此。 生成的图像如下所示: 我也想

plot [-10:10] sin(x + `echo $foo`)

但这会导致错误:

plot [-10:10] sin(x+)
                    ^
"./test.gp", line 9: invalid expression

我使用 gnuplot 4.6。

编辑:根据评论中的要求将解决方案移至单独的答案。

我尝试了 here 给出的建议,但它们似乎对我不起作用。 (虽然我被告知下面提供的解决方案实际上也在那里提到)。

我发现将脚本更改为:

#!/bin/bash


foo=42
gnuplot -e "foo='${foo}'" ./test.gp

效果更好。 GNUPLOT 脚本也变得更具可读性:

#!/usr/bin/gnuplot

set grid
set title "This is the title (".foo.")"

set terminal png large
set output "/tmp/test.png"
set samples 50, 50
plot [-10:10] sin(x + foo)