Idanis
Idanis

Reputation: 1988

How to call gnuplot from CLI and save the output graph to the image file?

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?

Upvotes: 8

Views: 26323

Answers (4)

Laura Segura
Laura Segura

Reputation: 11

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 is the .dat file that you will use for to graph...

It really works!

Upvotes: 1

robert4
robert4

Reputation: 1102

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).)

Upvotes: 2

Sunhwan Jo
Sunhwan Jo

Reputation: 2311

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

Upvotes: 8

andyras
andyras

Reputation: 15930

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

Upvotes: 11

Related Questions