[IPython-user] gnuplot variables

Arnd Baecker arnd.baecker at web.de
Mon Jul 28 13:56:41 CDT 2003


Hi,

the followig seems to boil down to the question:
  how to check if a pipe opened with popen3
  contains data to be read.

Still I thought I start to post this question
here as it is in the context of Fernando's improvements
to gnuplot...

Background:
There are a couple of interesting developments going on
in the CVS version of gnuplot. In particular,
with recent additions it is possible to get the coordinates
of a mouse click as variables in gnuplot:
  pause mouse "Click on one corner of desired zoom region"
  print "Mouse button ", MOUSE_BUTTON, " at ", MOUSE_X, " ", MOUSE_Y ;\

With this addition and the Python gnuplot support this
opens quite interesting possibilities.

However, there is one thing we would need from the python side,
namely, the ability to access the gnuplot variables from within python.
Presently `popen` is used to open a pipe to gnuplot,
however this is write-only, if I understand things correctly.
Now one could use popen2 or popen3 to also get the ouput.
Below is an example how far I got.
I am stuck with the following
  a) I think that after every gpwrite.flush() one should do
     a gpstderr.read() or gpstderr.readline()
      ((actually, I am not sure, what gpout contains,
       if anything))
     By this it should be possible to caputure
     any possible output.

     However, if there is no output, gpstderr.read()/gpstderr.readline()
     does not return and appears to be waiting for input ...

     Question: is there a way to check if gpstderr contains
     anything to be read ?
  b) If one feeds garbage to gnuplot, it seems that
     there are several lines in gpstderr...
     (if a) is solved, this should provide the solution for b) as
      well)

If anyone has an idea on a) I would be very grateful.

Further remark: I also tried popen2, but without success.

Best Arnd


################################################

# (kind-of) working example:
from os import popen,popen2,popen3

gpwrite,gpout,gpstderr=popen3("gnuplot")
gpwrite.write("set mouse\n")   # just for fun ;-)
gpwrite.flush()
gpwrite.write("plot sin(x)\n")
gpwrite.flush()
gpwrite.write("a=10\n")
gpwrite.flush()
gpwrite.write("b=11\n")
gpwrite.flush()
gpwrite.write("c=12\n")
gpwrite.flush()
gpwrite.write("d=14\n")
gpwrite.flush()

gpwrite.write("print a\n")
gpwrite.flush()
a_out=gpstderr.readline()

gpwrite.write("print b\n")
gpwrite.flush()
b_out=gpstderr.readline()

gpwrite.write("print c\n")
gpwrite.flush()
c_out=gpstderr.readline()

gpwrite.write("some_garbish_gnuplot_does_not_know\n")
gpwrite.flush()

gpwrite.write("print d\n")
gpwrite.flush()
d_out=gpstderr.readline()

print "a_out <%s>" % (a_out)
print "b_out <%s>" % (b_out)
print "c_out <%s>" % (c_out)
print "d_out <%s>" % (d_out)
gpstderr.readline()
gpstderr.readline()
gpstderr.readline()
gpstderr.readline()
gpstderr.readline()




More information about the IPython-user mailing list