我想用gnuplot从数据文件中绘制图形,比如 foo.data 。目前,我在命令文件中硬编码了数据文件名,比如 foo.plt ,并运行命令gnuplot foo.plg
来绘制数据。但是,我想将数据文件名作为命令参数传递,例如运行命令gnuplot foo.plg foo.data
。如何解析gnuplot脚本文件中的命令行参数?感谢。
答案 0 :(得分:171)
您可以通过开关-e
$ gnuplot -e "filename='foo.data'" foo.plg
在foo.plg中,您可以使用该变量
$ cat foo.plg
plot filename
pause -1
要使“foo.plg”更通用,请使用条件:
if (!exists("filename")) filename='default.dat'
plot filename
pause -1
答案 1 :(得分:45)
您可以在5.0版之后将参数传递给gnuplot脚本,标志为-c
。这些参数可通过变量ARG0
到ARG9
,ARG0
作为脚本以及ARG1
到ARG9
字符串变量来访问。参数的数量由ARGC
给出。
例如,以下脚本(" script.gp")
#!/usr/local/bin/gnuplot --persist
THIRD=ARG3
print "script name : ", ARG0
print "first argument : ", ARG1
print "third argument : ", THIRD
print "number of arguments: ", ARGC
可以被称为:
$ gnuplot -c script.gp one two three four five
script name : script.gp
first argument : one
third argument : three
number of arguments: 5
或在gnuplot中
gnuplot> call 'script.gp' one two three four five
script name : script.gp
first argument : one
third argument : three
number of arguments: 5
在gnuplot 4.6.6及更早版本中,存在一个call
机制,其语法不同(现已弃用)。通过$#
,$0
,...,$9
访问参数。例如,上面的相同脚本如下所示:
#!/usr/bin/gnuplot --persist
THIRD="$2"
print "first argument : ", "$0"
print "second argument : ", "$1"
print "third argument : ", THIRD
print "number of arguments: ", "$#"
在gnuplot中调用它(记住,版本< 4.6.6)
gnuplot> call 'script4.gp' one two three four five
first argument : one
second argument : two
third argument : three
number of arguments: 5
请注意,脚本名称没有变量,因此$0
是第一个参数,变量在引号内调用。没有办法直接从命令行使用它,只能通过@ con-fu-se建议的技巧。
答案 2 :(得分:26)
您还可以按照建议here通过环境传递信息。 Ismail Amin 的示例在此重复:
在shell中:
export name=plot_data_file
在Gnuplot脚本中:
#! /usr/bin/gnuplot
name=system("echo $name")
set title name
plot name using ($16 * 8):20 with linespoints notitle
pause -1
答案 3 :(得分:21)
Jari Laamanen的答案是最好的解决方案。我想解释一下如何在shell变量中使用多个输入参数:
output=test1.png
data=foo.data
gnuplot -e "datafile='${data}'; outputname='${output}'" foo.plg
和foo.plg:
set terminal png
set outputname
f(x) = sin(x)
plot datafile
正如您所看到的,更多参数以半冒号传递(如在bash脚本中),但字符串变量需要用''(gnuplot语法,非Bash语法)封装
答案 4 :(得分:14)
你可以在unix / linux环境中使用技巧:
在gnuplot程序中:绘制“/ dev / stdin”...
在命令行中:gnuplot program.plot< data.dat文件
答案 5 :(得分:10)
这个问题得到了很好的回答,但我认为无论如何我都可以找到一个适合填补这里的利基,如果只是为了减少像我一样用谷歌搜索的人的工作量。来自vagoberto的答案给了我解决我的问题所需的内容,所以我将在这里分享我的解决方案。
我在最新的环境中开发了一个绘图脚本,这使我可以这样做:
#!/usr/bin/gnuplot -c
set terminal png truecolor transparent crop
set output ARG1
set size 1, 0.2
rrLower = ARG2
rrUpper = ARG3
rrSD = ARG4
resultx = ARG5+0 # Type coercion required for data series
resulty = 0.02 # fixed
# etc.
在具有最新gnuplot(在我的情况下为5.0.3)的环境中,这可以从命令行完美地执行。
$ ./plotStuff.gp 'output.png' 2.3 6.7 4.3 7
当上传到我的服务器并执行时,它失败了,因为服务器版本是4.6.4(当前在Ubuntu 14.04 LTS上)。 下面的垫片解决了这个问题,无需对原始脚本进行任何更改。
#!/bin/bash
# GPlot v<4.6.6 doesn't support direct command line arguments.
#This script backfills the functionality transparently.
SCRIPT="plotStuff.gp"
ARG1=$1
ARG2=$2
ARG3=$3
ARG4=$4
ARG5=$5
ARG6=$6
gnuplot -e "ARG1='${ARG1}'; ARG2='${ARG2}'; ARG3='${ARG3}'; ARG4='${ARG4}'; ARG5='${ARG5}'; ARG6='${ARG6}'" $SCRIPT
这两个脚本的组合允许参数从bash传递到gnuplot脚本,而不考虑gnuplot版本和基本上任何* nix。
答案 6 :(得分:5)
你甚至可以做一些shell魔术,例如像这样:
#!/bin/bash
inputfile="${1}" #you could even do some getopt magic here...
################################################################################
## generate a gnuplotscript, strip off bash header
gnuplotscript=$(mktemp /tmp/gnuplot_cmd_$(basename "${0}").XXXXXX.gnuplot)
firstline=$(grep -m 1 -n "^#!/usr/bin/gnuplot" "${0}")
firstline=${firstline%%:*} #remove everything after the colon
sed -e "1,${firstline}d" < "${0}" > "${gnuplotscript}"
################################################################################
## run gnuplot
/usr/bin/gnuplot -e "inputfile=\"${inputfile}\"" "${gnuplotscript}"
status=$?
if [[ ${status} -ne 0 ]] ; then
echo "ERROR: gnuplot returned with exit status $?"
fi
################################################################################
## cleanup and exit
rm -f "${gnuplotscript}"
exit ${status}
#!/usr/bin/gnuplot
plot inputfile using 1:4 with linespoints
#... or whatever you want
我的实现有点复杂(例如在sed调用中替换一些魔法标记,而我已经在它...),但我简化了这个例子以便更好地理解。 你也可以让它更简单...... YMMV。
答案 7 :(得分:4)
在shell中写
gnuplot -persist -e "plot filename1.dat,filename2.dat"
并连续输出您想要的文件。 -persist用于使gnuplot屏幕保持不变,只要用户不手动退出它。
答案 8 :(得分:2)
另一种方式是:
您有一个名为scriptname.gp
的gnuplot脚本:
#!/usr/bin/gnuplot -p
# This code is in the file 'scriptname.gp'
EPATH = $0
FILENAME = $1
plot FILENAME
现在你可以通过这种错综复杂的语法来调用gnuplot脚本scriptname.gp
:
echo "call \"scriptname.gp\" \"'.'\" \"'data.dat'\"" | gnuplot
答案 9 :(得分:0)
@vagoberto的答案似乎是最好的恕我直言,如果您需要位置参数,那么我还有一点改进。
vagoberto的建议:
#!/usr/local/bin/gnuplot --persist
THIRD=ARG3
print "script name : ", ARG0
print "first argument : ", ARG1
print "third argument : ", THIRD
print "number of arguments: ", ARGC
被调用:
$ gnuplot -c script.gp one two three four five
script name : script.gp
first argument : one
third argument : three
number of arguments: 5
对于像我这样的懒惰打字员,可以使脚本可执行(chmod 755 script.gp
)
然后使用以下内容:
#!/usr/bin/env gnuplot -c
THIRD=ARG3
print "script name : ", ARG0
print "first argument : ", ARG1
print "third argument : ", THIRD
print "number of arguments: ", ARGC
并将其执行为:
$ ./imb.plot a b c d
script name : ./imb.plot
first argument : a
third argument : c
number of arguments: 4