【发布时间】:2015-12-03 15:17:41
【问题描述】:
使用某个程序(PersistenceLandscapes 工具箱)我正在生成大量脚本,我从中使用 gnuplot 生成图。我遍历文件并使用命令gnuplot gnuplotCommand.txt -p 使 gnuplot 显示绘图。如何让 gnuplot 将绘图保存为 PNG 或(最好是)EPS 格式? (我想避免干预gnuplotCommand-type 脚本。)
【问题讨论】:
标签: gnuplot
使用某个程序(PersistenceLandscapes 工具箱)我正在生成大量脚本,我从中使用 gnuplot 生成图。我遍历文件并使用命令gnuplot gnuplotCommand.txt -p 使 gnuplot 显示绘图。如何让 gnuplot 将绘图保存为 PNG 或(最好是)EPS 格式? (我想避免干预gnuplotCommand-type 脚本。)
【问题讨论】:
标签: gnuplot
你可以试试类似的 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
【讨论】:
plot 'gnuplotCommand.txt'更改为load 'gnuplotCommand.txt',因为该文件是其他程序自动生成的脚本。
最简单的解决方案是通过-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.txt、ARG2=pncairo 和ARG3=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 , ')'
}
【讨论】:
gnuplot -c script.gp "$(date +%F)"。这会将ARG1 设置为日期(在脚本中将其用作ARG1.".png")