gnuplot-py: plotoutput in string?

N

Nicola Kaiser

Hi,

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.

Is there any method in gnuplot.py that does this for me?


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

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?

Thanks,
Nicola
 
G

Grant Edwards

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top