使GNUPLOT从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选项添加终端设置,并通过管道将stdout发送到所需的输出文件:

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 , ')'
}

相关内容

最新更新