让 GNUPLOT 保存来自 shell 的绘图

Make GNUPLOT save a plot from shell

使用某个程序(PersistenceLandscapes 工具箱)我正在生成大量脚本,我从这些脚本中使用 gnuplot 生成绘图。我遍历文件并使用命令 gnuplot gnuplotCommand.txt -p 使 gnuplot 显示绘图。我怎样才能让 gnuplot 以 PNG 或(最好)EPS 格式保存绘图? (我想避免干扰 gnuplotCommand 类型的脚本。)

您可以尝试 bash 脚本,例如

gnuplot <<- EOF
    set term png
    set output 'gnuplotCommand.txt.png'
    load 'gnuplotCommand.txt'
EOF

或者,.eps 版本

gnuplot <<- EOF
    set terminal postscript eps
    set output 'gnuplotCommand.txt.eps'
    load 'gnuplotCommand.txt'
EOF

最简单的解决方案是通过 -e 选项添加终端设置,并将标准输出通过管道传输到所需的输出文件:

gnuplot -e 'set term pngcairo' gnuplotCommand.txt > output.png

如果您有 gnuplot 5.0 版,您可以将参数传递给您的脚本。例如,

# script.gp 
if (ARGC > 1) {
    set terminal ARG2
    set output ARG3

    print 'output file : ', ARG3, ' (', ARG2 , ')'
}

# load the script needed 
load ARG1

必须使用选项 -c 调用此脚本

gnuplot -c script.gp gnuplotCommand.txt pngcairo output.png

在这个例子中,我们设置了变量ARG1=gnuplotCommand.txtARG2=pncairoARG3=output.png。参数个数为 ARCG=3。此外,它已被设置为 ARG0=script.gp 作为主脚本的名称。

如果您只想查看输出,而不想将其保存到文件中,您可以将此脚本命名为:

gnuplot -p script.gp gnuplotCommand.txt

您可能需要检查用户是否为输出文件命名。如果没有,我们可以使用默认名称:

if (ARGC > 1) {
    if (ARGC < 3) { ARG3="default" }    # without extension... it doesn't matter in linux :)
    set terminal ARG2
    set output ARG3

    print 'output file : ', ARG3, ' (', ARG2 , ')'
}