Using a certain program (PersistenceLandscapes toolbox) I am generating a large number of scripts from which I generate plots with gnuplot. I iterate through the files and make gnuplot show the plot with the command gnuplot gnuplotCommand.txt -p. How can I make gnuplot save the plot in, say, PNG or (preferably) EPS format? (I want to avoid meddling with gnuplotCommand-type scripts.)
3 Answers
You could try a bash script like
gnuplot <<- EOF
set term png
set output 'gnuplotCommand.txt.png'
load 'gnuplotCommand.txt'
EOF
or, the .eps version
gnuplot <<- EOF
set terminal postscript eps
set output 'gnuplotCommand.txt.eps'
load 'gnuplotCommand.txt'
EOF
2 Comments
plot 'gnuplotCommand.txt' to load 'gnuplotCommand.txt', since this file is an auto-generated script from other program.The easiest solution is to add the terminal settings via the -e option, and pipe the stdout to the desired output file:
gnuplot -e 'set term pngcairo' gnuplotCommand.txt > output.png
1 Comment
If you have gnuplot version 5.0, you can pass arguments to your script. For example,
# script.gp
if (ARGC > 1) {
set terminal ARG2
set output ARG3
print 'output file : ', ARG3, ' (', ARG2 , ')'
}
# load the script needed
load ARG1
This script must be called with option -c
gnuplot -c script.gp gnuplotCommand.txt pngcairo output.png
In this example, we have set the variables ARG1=gnuplotCommand.txt, ARG2=pncairo, and ARG3=output.png. The number of arguments is ARCG=3. Also, it has been set ARG0=script.gp as the name of the main script.
If you just want to see the output, without saving it to a file, you can call this script as:
gnuplot -p script.gp gnuplotCommand.txt
You may want to check if the user has given a name for the output file. If not, we can use a default name:
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 , ')'
}
4 Comments
gnuplot -c script.gp "$(date +%F)". This will set ARG1 as the date (use it as ARG1.".png" inside the script)