|
From: Noel O'B. <no...@ca...> - 2005-11-11 09:46:54
|
Dear Michael,
I have two questions relating to writing graphs to disk. The first is
whether it is possible to do so without first plotting the image on the
screen.
The second is related to the following problem with using popen, with
which I think you are familiar:
*************************************************
import Gnuplot,os
error = 0
for i in range(50):
g = Gnuplot.Gnuplot()
d = Gnuplot.Func("sin(x)")
g.plot(d)
g.hardcopy("gnuplotpy.png",terminal="png")
try:
assert os.path.getsize("gnuplotpy.png")>0
except AssertionError:
error += 1
print error
*************************************************
On one run, 27 AssertionErrors were raised out of 50, the problem being
that the Python program continues while Gnuplot is still trying to plot
the image. This is a particular problem when the Python program ends
soon after calling Gnuplot, as the Gnuplot image is often not written to
disk.
There seems to be no way around this using the popen family of commands
apart from waiting until the file is created:
*************************************************
size = os.path.getsize("gnuplotpy.png")
while size==0:
time.sleep(0.2)
size = os.path.getsize("gnuplotpy.png")
*************************************************
If only Gnuplot returned an exit signal to the calling program, then you
could use popen3 or something and wait for stdout to be closed.
Unfortunately, this doesn't appear to be the case.
On the other hand, the following approach addresses both of the problems
listed above, and I am afraid that it is difficult for me to convince
myself that using gnuplot-py would be better (for me):
*************************************************
import os
errors = 0
for i in range(50):
cmdfile = open("cmd.txt","w")
cmdfile.write('set terminal png\nset output "gnuplotpy.png"\n')
cmdfile.write("plot sin(x)\n")
cmdfile.close()
os.system("pgnuplot.exe cmd.txt")
try:
assert os.path.getsize("gnuplotpy.png")>0
except AssertionError:
errors += 1
print errors
*************************************************
errors is always zero, as os.system does not return until the command is
completed.
Would you consider including os.system in some way in gnuplot-py?
Regards,
Noel
|