Gnuplot woes.

Michael Haggerty mhagger at
Mon May 22 19:24:00 CEST 2000

Jacek Generowicz <jmg at> 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( '' )
> -------------------------
> [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 and gnuplot is one-way, there
is no way for to know how long the hardcopy() takes to

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

       history = []
       while string.strip(command) != 'quit':
      	   data = Gnuplot.Data( [[1,1],[2,2],[3,3],[4,4]] )
      	   g.hardcopy( '' )
      	   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( '' )

   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 Haggerty
mhagger at

More information about the Python-list mailing list