0

There's a certain behaviour of pylab that I do not understand. I have written a program that calculates some numbers and writes them to the standard output. After that, show() is called in the same program which gives me a graphical output. When I write

program.py > tmp_file

it opens the graph and writes the numbers to tmp_file. So far, so good. Now, I would like to do the same thing, but automized. But, if I write

program.py > tmp_file &

the konsole is ready for the next commands, but the tmp_file remains empty until the graph is shown (although the show() command follows the print command in program.py, not the other way round). As it takes a while for the graph to be shown, using the tmp_file for further processes is impossible. For example,

program.py > tmp_file &
cat tmp_file

gives an empty output, while

program.py > tmp_file
cat tmp_file

gives the expected resulting values.

What am I doing wrong? Is there a solution to this problem?

I am using the suse standard python installation 2.7.3.

Another question I could not find an answer to: is it possible to write the show() output to stdout? Like program.py > graph.xxx?

Thanks for your help!

  • This has little to do with Pylab. It's a feature called buffering, and is mostly due to the OS. It's convenient to store output for example in memory, and then write it all at once to disk. You can use things like `sys.stdout.flush`, but you may first want to think if that's really necessary. An extended example is [here](http://stackoverflow.com/questions/107705/python-output-buffering). –  Mar 11 '14 at 10:09
  • To save the figure to disk, use [savefig](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.savefig) (that's only 1 Google away). Are you really sure you want to *redirect* the matplotlib output to a file. Because you can, but it's a bit of an effort. –  Mar 11 '14 at 10:12
  • Thanks for pointing in the right direction. I have tried sys.stdout.flush, but it does not solve the problem. I also tried the sys.stdout =... line, but it also does not work. Also the -u program option of python does not solve the problem. But as you have redirected my question, I will do some more research and consider the original question to be answered. – user3405420 Mar 11 '14 at 11:02
  • To the second question: I meant the redirection, it makes it possible to choose the filename without changing the program. Could you give a short answer to that as well, please? – user3405420 Mar 11 '14 at 11:04
  • I even doubt now that the problem has anything to do with buffering; even when I run "PYTHONUNBUFFERED=true" before executing "program.py > tmp_file & cat tmp_file", nothing changes. Please help! – user3405420 Mar 11 '14 at 11:29
  • Without seeing actual code, it's hard to know what's really causing your problem then. –  Mar 11 '14 at 12:19
  • `program.py > tmp_file & cat tmp_file` won't work. `cat tmp_file` will be executed nearly at the same time `program.py` has started, so you're still catting an empty file: the Python interpreter will barely have had time to start up properly by the time you looked at the file, let alone that any writing will have started. Try `tail -f tmp_file` instead. –  Mar 11 '14 at 12:30
  • Of course. This answers the question, thank you. – user3405420 Mar 12 '14 at 08:30

1 Answers1

0

For your second question, program.py > tmp_file use

import sys
import pylab

pylab.plot([2,4,6,1,3,5])
pylab.savefig(sys.stdout, format='png')

You still have to specify the format explicitly: there is no way matplotlib can read the filename and deduce the format from it.

But it's better to make the output filename an argument; then matplotlib can use the actual filename and automatically determine the format:

import sys
import pylab

pylab.plot([2,4,6,1,3,5])
pylab.savefig(sys.argv[1])

To be run as program.py tmp_file.png.

  • The first solution is exactly what I wanted, thank you. Closed. – user3405420 Mar 12 '14 at 08:32
  • @user3405420 I recommend the second solution, unless you intend to pipe the output to another program. Otherwise on occasions, you may end up with a bunch of garbage on your screen if you run the program without a redirect. –  Mar 12 '14 at 12:59
  • @user3405420 If you consider the question answered, you can "close" it by ticking the correct answer (it's still open for answers from other people). –  Mar 12 '14 at 13:00