Gnuplot woes.

Michael Haggerty mhagger at alum.mit.edu
Mon May 22 13:24:00 EDT 2000


Jacek Generowicz <jmg at ecs.soton.ac.uk> writes:
> The following program:
> 
> ------------------------
> import Gnuplot
> import string
> 
> g = Gnuplot.Gnuplot()
> g('set data style lines')
> 
> command = ''
> while string.lstrip(string.rstrip(command)) !=
> 'quit':
> 
>     g.plot( [[1,1],[2,2],[3,3],[4,4]] )
>     command = raw_input('plot> ')
>     g.hardcopy( 'hmm.ps' )
> -------------------------
> [fails.]

The reason that the program fails is that g.hardcopy() needs the
temporary file created by g.plot().  But that temporary file is
deleted the moment the subsequent g.plot() is called.  (Temporary
files are deleted when the associated PlotItem is deleted, which
occurs in this case as soon as the next time g.plot() is called.)
Since communication between Gnuplot.py and gnuplot is one-way, there
is no way for Gnuplot.py to know how long the hardcopy() takes to
finish.

Solutions (untested):

1. Put a delay AFTER any hardcopy() command, and indeed after any
   gnuplot command that might take a while.  The delay allows gnuplot
   to read the temporary file before Gnuplot.py deletes it.

2. Control the lifetime of your data explicitly by putting it in a
   PlotItem and not deleting the PlotItem until a while has passed.
   E.g.,

       history = []
       while string.strip(command) != 'quit':
      	   data = Gnuplot.Data( [[1,1],[2,2],[3,3],[4,4]] )
      	   g.plot(data)
      	   history.append(data)
      	   g.hardcopy( 'hmm.ps' )
      	   command = raw_input('plot> ')
       del history

3. Use the 'inline' feature:

       while string.strip(command) != 'quit':
      	   g.plot(Gnuplot.Data( [[1,1],[2,2],[3,3],[4,4]], inline=1 ))
      	   command = raw_input('plot> ')
      	   g.hardcopy( 'hmm.ps' )

   This causes the data to be passed to gnuplot `inline' (through the
   same pipe as the commands).  This should prevent synchronization
   problems (and avoid the use of temporary files).  However, it may
   be slower for some big data sets because python needs to write the
   data twice (once for the plot and once for the hardcopy).

I suppose this should be explained better in the documentation.

Good Luck,
Michael

-- 
Michael Haggerty
mhagger at alum.mit.edu



More information about the Python-list mailing list