gnuplot-py: plotoutput in string?

Grant Edwards grante at visi.com
Thu Aug 18 12:05:50 EDT 2005


On 2005-08-18, Nicola Kaiser <Nicola.Kaiser at urz.uni-heidelberg.de> wrote:

> I´m using Gnuplot via gnuplot.py and I´m looking for a way to get the 
> plotting output (terminal set to png in my case) piped in a string 
> instead of to stdout or a file.

Plotting to a file and then reading the file is pretty trivial:

  import Gnuplot,time

  filename = 'foo.png'
  p = Gnuplot.Gnuplot()
  p('set term png')
  p('set out "%s"' % filename)
  p.plot('sin(x)')
  p('set out')
  time.sleep(0.1)  # wait for gnuplot to process all the commands
  s = open(filename,'rb').read()

Since gnuplot is running asycnronously and reading commands
from a pipe, you've got to give it some time to finish
executing the commands before you attempt to read the file.
  
> Is there any method in gnuplot.py that does this for me?

I don't think so.

> If not, I tried something like:
>
> p=Gnuplot.Gnuplot(debug=1)
> p('set terminal png')
> oldstdout=sys.stdout
> output=cStringIO.StringIO()
> sys.stdout=output
> p.plot([[1,0],[2,50],[3,0],[4,20],[5,0],[6,30],[7,70], [8,70], [9, 75], 
> [10, 50], [11, 30], [12, 50]])
> sys.stdout=oldstdout
> print output.getvalue()

That's not going to work.

The gnuplot engine isn't a Python program.  It's not writing to
sys.stdout, it's writing to Unix file descriptor 1.  In order
to do things the way you're attempting to, you'd have to
redirect file descriptor 1 to a pipe before you start the
gnuplot engine, then read the plot data from the pipe.

The Gnuplot module may already be redirecting the gnuplot
processes output file descriptors, so attempting to do it
yourself might not work anyway.

> But unfortunately I also get other stuff like:
>
> "gnuplot: unable to open display ''
> "gnuplot: X11 aborted"
>
> in the same string
>
> Can anyone help me on this?

You shouldn't have gotten messages about the X11 terminal
driver if you really had set the terminal type to png.

-- 
Grant Edwards                   grante             Yow!  Are you selling NYLON
                                  at               OIL WELLS?? If so, we can
                               visi.com            use TWO DOZEN!!



More information about the Python-list mailing list