Controlling gnuplot via subprocess.Popen


P

Peter Beattie

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?
 
Ad

Advertisements

B

Ben C

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

James Stroud

Peter said:
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?

Why re-invent the wheel? Not trying to be pedantic, but usually the
google search

"python %s" % thing_im_working_on

will yield lucrative results that far exceed anything you can create in
a reasonable amount of time. For example, "python gnuplot" first hit is:

http://gnuplot-py.sourceforge.net/

And speaking from experience, it works beautifully.

James
 
Ad

Advertisements

G

Grant Edwards

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?

wgnuplot.exe doesn't read commands from stdin. However,
pgnuplot.exe does (that's why it exists).

It's probably easier to just use the gnuplot-py module:

http://gnuplot-py.sourceforge.net/

The "released" 1.7 version still uses Numeric. I believe
that it's been converted over to numpy, so if you prefer numpy
over Numeric, you can grab an SVN snapshot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top