153

我想使用 gnuplot 从数据文件中绘制图形,比如foo.data。目前,我在命令文件中硬编码数据文件名,比如foo.plt,然后运行命令gnuplot foo.plg来绘制数据。但是,我想将数据文件名作为命令参数传递,例如 running command gnuplot foo.plg foo.data。如何解析 gnuplot 脚本文件中的命令行参数?谢谢。

4

10 回答 10

202

您可以通过开关输入变量-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

请注意,-e必须在文件名之前,否则文件在-e语句之前运行。特别是,#!/usr/bin/env gnuplot使用./foo.plg -e ...CLI 参数运行 shebang gnuplot 将忽略使用提供的参数。

于 2012-09-08T11:49:38.037 回答
68

从 5.0 版开始,您可以使用标志将参数传递给 gnuplot 脚本-c。这些参数是通过变量ARG0来访问的ARG9ARG0即脚本,以及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 建议的技巧。

于 2015-08-04T16:54:32.120 回答
28

您还可以按照此处的建议通过环境传递信息。Ismail Amin的例子在这里重复:

在外壳中:

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
于 2012-09-08T10:24:04.703 回答
23

Jari Laamanen 的回答是最好的解决方案。我只想解释如何将 1 个以上的输入参数与 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 语法)

于 2014-06-17T20:29:03.393 回答
14

您可以在 unix/linux 环境中使用技巧:

  1. 在 gnuplot 程序中: plot "/dev/stdin" ...

  2. 在命令行中:gnuplot program.plot < data.dat

于 2013-12-09T12:20:02.597 回答
12

这个问题得到了很好的回答,但我想我可以在这里找到一个合适的位置来填补,如果只是为了减少像我一样在谷歌上搜索的人的工作量。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。

于 2016-10-27T06:01:23.827 回答
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。

于 2013-12-06T17:11:12.497 回答
5

在shell中写

gnuplot -persist -e "plot filename1.dat,filename2.dat"

并连续获得您想要的文件。-persist 用于使 gnuplot 屏幕保持不变,只要用户不手动退出它。

于 2017-04-20T15:16:47.170 回答
2

如果您需要位置参数, @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
于 2020-03-18T19:56:27.943 回答
1

还有一种方式是这样的:

您有一个名为的 gnuplot 脚本scriptname.gp

#!/usr/bin/gnuplot -p
# This code is in the file 'scriptname.gp'
EPATH = $0
FILENAME = $1 

plot FILENAME

scriptname.gp现在您可以通过这种复杂的语法和平调用 gnuplot 脚本:

echo "call \"scriptname.gp\" \"'.'\" \"'data.dat'\"" | gnuplot 
于 2014-06-21T14:09:30.613 回答