问题
I'm writing a batch file which will also generate a gnuplot graph from a dat file.
I wish to call gnuplot from the command line, using the gnuplot "gnu" script I have written, and save the output graph to an image.
Something like:
gnuplot.exe script.gnu > image.png
Any ideas?
回答1:
Simply putting the following line will make gnuplot to return png-format bytecode. Thus, you can redirect the output to a png-file.
set terminal png
回答2:
You don't have to redirect the output from gnuplot into an image file; you can set that inside the gnuplot script itself:
set terminal png
set output 'image.png'
If you want to have a variable output name, one simple way to do that in bash is to wrap the gnuplot commands thus:
#!/bin/bash
echo "set terminal png
set output '$1'
plot 'data.dat'" | gnuplot
This way you can run the bash script with an argument for the output file name:
./plotscript.sh image.png
回答3:
When your batch file runs gnuplot only (with the script) and does nothing else, then you can combine the batch file with the gnuplot script:
@echo off & call gnuplot -e "echo='#';set macros" "%~f0" & goto :eof
set terminal png
set output 'image.png'
...
Save this with .cmd extension.
The advantages of this is that:
- you have only 1 file instead of two
- that file is runnable from Explorer/TaskScheduler/etc. in a portable way
(no need to associate a new extension with gnuplot.exe)
In other words, this is the Windows "equivalent"(?) of the #!/usr/bin/env gnuplot
solution of Unix
(this is why I find it so comfortable when working with gnuplot scripts under Windows).
(Note: 'call gnuplot' is used to allow for a gnuplot.cmd file somewhere in the PATH -- as opposed to polluting the PATH with the folder of gnuplot.exe (and many other programs).)
回答4:
The previous solutions doesn't work, you need to implement:
First: create the script.sh like this:
!/bin/sh
gnuplot << EOF
set terminal postscript eps color enhanced
set output "$1.eps" # all the declarations that you need
set xlabel "Energy [MeV]"
plot "$1.dat" using 1:2 notitle w l
EOF
Second: execute the script:
$ ./script.sh data
*the parameter data es the .dat file that you will use for to graph...
It really works!
来源:https://stackoverflow.com/questions/14047061/how-to-call-gnuplot-from-cli-and-save-the-output-graph-to-the-image-file