Controlling gnuplot via subprocess.Popen

Ben C spamspam at spam.eggs
Wed Apr 25 17:14:31 EDT 2007


On 2007-04-25, Peter Beattie <Peter.Beattie at web.de> wrote:
> I am trying to plot something in gnuplot 4.2 using co-ordinates a Python
> 2.5 program computes. Here's what I'm doing:
>
> py> from subprocess import *
> py> plot = Popen("c:/progs/gp/bin/wgnuplot.exe", stdin=PIPE)
> py> plot.stdin.write("plot x*x")
>
> The first command dutifully opens gnuplot, but the second doesn't do
> anything. Could someone favour me with an explanation as to the whyness?

I think it may just be that you need a newline after "plot x*x", i.e.

    plot.stdin.write("plot x*x\n")

or

    print >>plot.stin, "plot x*x"

But some interactive programs need to be controlled with expect rather
than just writing to their stdin. I'm unclear of the details, perhaps
it's just ones that use curses in some form.

I usually write the gnuplot commands to a file, and then use
os.system("gnuplot plot.gpi") to run gnuplot in batch mode (or gnuplot
-persist if you want the window). You can also use Popen instead of
os.system.



More information about the Python-list mailing list