Calling gnuplot from python

Andrei Ciobanu picture Andrei Ciobanu · Jan 29, 2010 · Viewed 44.7k times · Source

I've a python script that after some computing will generate two data files formatted as gnuplot input.

How do I 'call' gnuplot from python ?

I want to send the following python string as input to gnuplot:

"plot '%s' with lines, '%s' with points;" % (eout,nout)

where 'eout' and 'nout' are the two filenames.

PS: I prefer not to use additional python modules (eg. gnuplot-py), only the standard API.

Thank You

Answer

sth picture sth · Jan 29, 2010

The subprocess module lets you call other programs:

import subprocess
plot = subprocess.Popen(['gnuplot'], stdin=subprocess.PIPE)
plot.communicate("plot '%s' with lines, '%s' with points;" % (eout,nout))