Help sending a PNG from matplotlib to stdout

Having some issues getting a png to flow out to a web page. I am trying to
use savefig( sys.stdout ), but I am getting an error. Well at least I can
show the error when I run the script from a command prompt.

I took most of the example from the matplotlib pdf file. I added a couple
print statements in order to get more feedback from the web server.

When run from the command prompt in Windows, My output looks like this:
hello world<br>
This is the second line.<br>
Traceback (most recent call last):
  File "C:\www\app\webroot\cgi-bin\stdout.py", line 12, in <module>
    savefig( sys.stdout )
  File
"C:\Python25\lib\site-packages\matplotlib-0.91.2.0001-py2.5-win32.egg\matplo
tlib\pyplot.py", line 269, in savefig
    return fig.savefig(*args, **kwargs)
  File
"C:\Python25\lib\site-packages\matplotlib-0.91.2.0001-py2.5-win32.egg\matplo
tlib\figure.py", line 782, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File
"C:\Python25\lib\site-packages\matplotlib-0.91.2.0001-py2.5-win32.egg\matplo
tlib\backend_bases.py", line 1195, in print_figure
    **kwargs)
  File
"C:\Python25\lib\site-packages\matplotlib-0.91.2.0001-py2.5-win32.egg\matplo
tlib\backends\backend_agg.py", line 397, in print_png
    self.get_renderer()._renderer.write_png(str(filename),
self.figure.dpi.get())
RuntimeError: Could not open file <open file '<stdout>', mode 'w' at
0x00A0F068>

···

---

Here is some version info:
Windows XP Pro
Python 2.5.2
matplotlib-0.91.2.0001-py2.5-win32.egg
numpy-1.0.4.0002-py2.5-win32.egg

My entire script is listed below:
#!c:/Python25/python.exe -u
print
print "hello world<br>"
import sys
import matplotlib
# this is not supported across all backends , as of matplotlib -0.63
matplotlib.use('Agg')
from pylab import *
print "This is the second line.<br>"
plot ([1 ,2 ,3])
savefig( sys.stdout )
---

When I put this script in the cgi directory and refer to it in my URL, the
response only shows Hello World. The "This is the second line." never
displays. Seems that as soon as I import matplotlib I am not able to print
to stdout.

Any suggestions would be appreciated.
-Rodney

I believe this has been fixed on the maintenance branch (which will
become 0.91.3 shortly after the next release). You can get a svn
checkout of this branch at

svn co matplotlib download | SourceForge.net
mpl91

Michael, I notice in the code we have:

        if type(filename_or_obj) in (str, unicode):
            filename_or_obj = open(filename_or_obj, 'w')

is there any reason not to use cbook.is_string_like here, which should
be more general?

Thanks,
JDH

···

On Sat, May 17, 2008 at 3:49 PM, Rodney Haynie <RHaynie@...614...> wrote:

"C:\Python25\lib\site-packages\matplotlib-0.91.2.0001-py2.5-win32.egg\matplo
tlib\backends\backend_agg.py", line 397, in print_png
   self.get_renderer()._renderer.write_png(str(filename),
self.figure.dpi.get())
RuntimeError: Could not open file <open file '<stdout>', mode 'w' at
0x00A0F068>

maintenance branch (which will

become 0.91.3 shortly after the next release). You can get a svn >
checkout of this branch at

svn co
matplotlib download | SourceForge.net
91_maint
mpl91

Great, I got the files through svn.
I have the Enthought version of python installed (2.5.2) with all the
packages that come with enstall.

Would you please point me in the right direction for getting matplotlib
built and installed?

I brought the files down to c:\mpl91.

So, I opened a command prompt in windows and navigated to that folder.

Then I typed: setup.py build

During the build process it gave me some errors. Below I have pasted the
output from the above call. Do I need vs2003 installed on my box?

···

From: John Hunter [mailto:jdh2358@…287…]
Sent: Saturday, May 17, 2008 4:56 PM> I believe this has been fixed on the

============================================================================
BUILDING MATPLOTLIB
            matplotlib: 0.91.2svn
                python: 2.5.2 |EPD 2.5.2001| (release25-maint:60919M, Feb 21
                        2008, 10:31:43) [MSC v.1310 32 bit (Intel)]
              platform: win32
       Windows version: (5, 1, 2600, 2, 'Service Pack 2')

REQUIRED DEPENDENCIES
                 numpy: 1.0.4
             freetype2: found, but unknown version (no pkg-config)
                        * WARNING: Could not find 'freetype2' headers in any
                        * of '.', '.\freetype2'.

OPTIONAL BACKEND DEPENDENCIES
                libpng: found, but unknown version (no pkg-config)
                        * Could not find 'libpng' headers in any of '.'
               Tkinter: no
                        * Tkinter present, but header files are not found.
                        * You may need to install development packages.
              wxPython: 2.8.4.0
                        * WxAgg extension not required for wxPython >= 2.8
                  Gtk+: no
                        * Building for Gtk+ requires pygtk; you must be able
                        * to "import gtk" in your build/install environment
                    Qt: no
                   Qt4: no
                 Cairo: no

OPTIONAL DATE/TIMEZONE DEPENDENCIES
              datetime: present, version unknown
              dateutil: 1.3
                  pytz: 2006p

OPTIONAL USETEX DEPENDENCIES
                dvipng: file.
           ghostscript: 'gswin32c' is not recognized as an internal or
                        external command, operable program or batch file.
                 latex: no

EXPERIMENTAL CONFIG PACKAGE DEPENDENCIES
             configobj: 4.5.1
      enthought.traits: 2.0.4

[Edit setup.cfg to suppress the above messages]

running build
running build_py
copying lib\matplotlib\mpl-data\matplotlibrc ->
build\lib.win32-2.5\matplotlib\mpl-data
copying lib\matplotlib\mpl-data\matplotlib.conf ->
build\lib.win32-2.5\matplotlib\mpl-data
running build_ext
No module named msvccompiler in numpy.distutils; trying from distutils
error: Python was built with Visual Studio 2003; extensions must be built
with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin
installed, you can try compiling with MingW32, by passing "-c mingw32" to
setup.py.

Thanks
-Rodney

John Hunter wrote:

···

On Sat, May 17, 2008 at 3:49 PM, Rodney Haynie <RHaynie@...614...> wrote:

"C:\Python25\lib\site-packages\matplotlib-0.91.2.0001-py2.5-win32.egg\matplo
tlib\backends\backend_agg.py", line 397, in print_png
   self.get_renderer()._renderer.write_png(str(filename),
self.figure.dpi.get())
RuntimeError: Could not open file <open file '<stdout>', mode 'w' at
0x00A0F068>
    
I believe this has been fixed on the maintenance branch (which will
become 0.91.3 shortly after the next release). You can get a svn
checkout of this branch at

svn co matplotlib download | SourceForge.net
mpl91

Michael, I notice in the code we have:

        if type(filename_or_obj) in (str, unicode):
            filename_or_obj = open(filename_or_obj, 'w')

is there any reason not to use cbook.is_string_like here, which should
be more general?
  

I don't think so. Probably just an oversight on my part. I'll correct it.

Cheers,
Mike

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Rodney Haynie wrote:

From: John Hunter [mailto:jdh2358@…287…] Sent: Saturday, May 17, 2008 4:56 PM> I believe this has been fixed on the
maintenance branch (which will
  

become 0.91.3 shortly after the next release). You can get a svn > checkout of this branch at

svn co matplotlib download | SourceForge.net
91_maint
mpl91
    
Great, I got the files through svn.
I have the Enthought version of python installed (2.5.2) with all the
packages that come with enstall.

Would you please point me in the right direction for getting matplotlib
built and installed?

I brought the files down to c:\mpl91.

So, I opened a command prompt in windows and navigated to that folder.

Then I typed: setup.py build

During the build process it gave me some errors. Below I have pasted the
output from the above call. Do I need vs2003 installed on my box?

No, but you do need a compiler supported by Python's distutils. If you want to use a non-Microsoft compiler, you have to explicitly say so when you invoke setup.py with the "--compiler" flag. I personally have had success with mingw32. Once you have that installed and setup, there are additional Windows build instructions at the top of setupext.py.

Let us know how you progress.

Cheers,
Mike

···

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA