views:

625

answers:

2

I've a python script that after some computing is a generating 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

+6  A: 

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))
sth
After reading your example, I've written a similar function, unfortunately, no results.(POpen = Popen, a typo I believe, but this wasn't the problem)
Andrei Ciobanu
Yes, POpen was a typo. Aside from that, maybe you need to specify the full path to gnuplot or add the '-persist' switch you mention in another comment. You can also check `plot.returncode` for errors.
sth
+1  A: 

A simple approach might be to just write a third file containing your gnuplot commands and then tell Python to execute gnuplot with that file. Say you write

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

to a file called tmp.gp. Then you can use

from os import system, remove
system('gnuplot tmp.gp')
remove('tmp.gp')
Neal
Thanks, this hack eventually worked.One mention: system('gnuplot -persist tmp.gp') in order to persist the windows after the script finishes.
Andrei Ciobanu