2

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 3

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

change plot 'gnuplotCommand.txt' to load 'gnuplotCommand.txt', since this file is an auto-generated script from other program.
I think you just answered the question I asked in the comment above. I need to use system("date +F").png as my filename. I know how to do that in two lines of code. TNX.
2

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

This is handy. I just use two versions of the same plot program. One for pngcairo and one for X11 with pause and reread for the screen. But this is nice to know. Never though to try to pipe to a png file like this, either. My output files need to be named by date, though, so somehow I need to set the filename to -- system("date +%F:).png -- would I just use a \n after the set term in order to add more lines, or perhaps the naming can be done in bash? My hat is off to you, @Christoph.
2

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

Arguments can be passed in earlier versions too, but use a different syntax.
This looks good, but what if the argument needs to be either today's date or perhaps an arbitrary one? Inside the program I would use date=system("date +%F") then set output date.".png" to get today's date.
@SDsolar Try with gnuplot -c script.gp "$(date +%F)". This will set ARG1 as the date (use it as ARG1.".png" inside the script)
Thank you for this. I am getting it, and appreciate the guidance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.