I want to use gnuplot to draw figure from data file, say foo.data. Currently, I hardcoded the data file name in the command file, say foo.plt, and run command gnuplot foo.plg
to plot data. However, I want to pass the data file name as a command argument, e.g. running command gnuplot foo.plg foo.data
. How to parse the command line arguments in gnuplot script file? Thanks.
相关问题
- How to set a variable line width when plotting?
- How to create PNG images with more than 72dpi usin
- softlinks atime and mtime modification
- Get unexpanded argument from bash command line
- Include and Execute EXE in C# Command Line App
相关文章
- Compile and build with single command line Java (L
- How to update command line output?
- How to execute another python script from your scr
- Python file keyword argument?
- Interactively merge files tracked with git and unt
- Lauch default editor (like 'webbrowser' mo
- Gnuplot - Using replot with png terminal
- How to print jq output sequentially
You may use trick in unix/linux environment:
in gnuplot program: plot "/dev/stdin" ...
In command line: gnuplot program.plot < data.dat
You can pass arguments to a gnuplot script since version 5.0, with the flag
-c
. These arguments are accessed through the variablesARG0
toARG9
,ARG0
being the script, andARG1
toARG9
string variables. The number of arguments is given byARGC
.For example, the following script ("script.gp")
can be called as:
or within gnuplot as
In gnuplot 4.6.6 and earlier, there exists a
call
mechanism with a different (now deprecated) syntax. The arguments are accessed through$#
,$0
,...,$9
. For example, the same script above looks like:and it is called within gnuplot as (remember, version <4.6.6)
Notice there is no variable for the script name, so
$0
is the first argument, and the variables are called within quotes. There is no way to use this directly from the command line, only through tricks as the one suggested by @con-fu-se.You can also pass information in through the environment as is suggested here. The example by Ismail Amin is repeated here:
In the shell:
In a Gnuplot script:
You can input variables via switch
-e
In foo.plg you can then use that variable
To make "foo.plg" a bit more generic, use a conditional:
This question is well answered but I think I can find a niche to fill here regardless, if only to reduce the workload on somebody googling this like I did. The answer from vagoberto gave me what I needed to solve my version of this problem and so I'll share my solution here.
I developed a plot script in an up-to-date environment which allowed me to do:
This executes perfectly well from command-line in an environment with a recent gnuplot (5.0.3 in my case).
When uploaded to my server and executed, it failed because the server version was 4.6.4 (current on Ubuntu 14.04 LTS). The below shim solved this problem without requiring any change to the original script.
The combination of these two scripts allows parameters to be passed from bash to gnuplot scripts without regard to the gnuplot version and in basically any *nix.
The answer of Jari Laamanen is the best solution. I want just explain how to use more than 1 input parameter with shell variables:
and foo.plg:
As you can see,more parameters are passed with semi colons (like in bash scripts), but string variables NEED to be encapsuled with ' ' (gnuplot syntax, NOT Bash syntax)