[IPython-user] gnuplot variables

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


Hi,

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 ;-)

Arnd

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 0.0.0.1, 31.07.2003, Arnd Baecker
"""

from os import popen2

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

    def __init__(self):
        """Create a pipe to gnuplot"""
        self.gpwrite,self.gpout=popen2("gnuplot")

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

        self.gpwrite.write(s+"\n")
        self.gpwrite.flush()

    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" %
(var,var))
        result=self.gpout.readline()
        self("set print\n")            # print output to default stderr
        return(result)


        self("print %s" % (var))
        result=self.gpout.readline()



if (__name__=="__main__"):
    gp=Gnuplot()
    gp("plot sin(x)")
    gp("a=10")
    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

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





More information about the IPython-user mailing list