[IPython-user] gnuplot variables

Arnd Baecker arnd.baecker at web.de
Thu Jul 31 15:31:57 CDT 2003


I think I am a step further now - below is a version which
does work for me (i.e under linux). I have no idea about
windows and in any case it needs polishing ...

The crucial point is to redirect print to stdout
so that it does not mix with error messages sent to stderr.
(Ethan Merrit pointed out that by "set print ...."
you can redirect the print output).

Maybe the stuff below is already useful to someone
in its present form - it is a quick shot and thus a bit rough
and I appreciate any comments
(there will be a > 3 weeks delay in replies on my side
due to a _real_ holiday ;-)


P.S.: you'll need a very recent CVS gnuplot + --enable-isvar
during configure (see below).


Test of Bidirectional Interfacing with gnuplot.
Main feature: get variables from (very recent CVS) gnuplot.

No capture of stderr (using popen3 should allow this....)

Version, 31.07.2003, Arnd Baecker

from os import popen2

class Gnuplot:
    """Interface to gnuplot."""

    def __init__(self):
        """Create a pipe to gnuplot"""

    def __call__(self, s):
        """Send string to gnuplot."""


    def getvar(self,var):
        """get a gnuplot variable

           In order to avoid locking we have to check
           if the variable is really defined.
           This only works with a recent CVS gnuplot,
           with ./configure   --enable-isvar
           which  enables defined(var) function (EXPERIMENTAL)

           NOTICE: this flag will renamed
           according a recent discussion on the gnuplot-dev
           mailing list.

        self("set print \"-\"\n")      # print output to stdout
        self("if (defined(%s)) print %s ; else print \"None\" \n" %
        self("set print\n")            # print output to default stderr

        self("print %s" % (var))

if (__name__=="__main__"):
    gp("plot sin(x)")
    print "var=",gp.getvar("a")
    print "var=",gp.getvar("b")   # This one has not been defined ...
                                  # ... and we get "None"
                                  # (maybe one should raise an error here

    print "var=",gp.getvar("c")

More information about the IPython-user mailing list