1

I am trying to test whether matplotlib installed properly in python3. I am following this basic tutorial: http://www.scipy.org/Plotting_Tutorial

This question is similar to one asked previously but unfortunately did not report the final solution explicitly and this is a different operating system.

The operating system for this is Mac OS X 10.6.8. Running the script in Python 3.2 does not work, but running it in Python 2.7 does. Unfortunately I need 3.2.

When the savefig line is commented out, it works fine.

"""
Example: simple line plot.
Show how to make and save a simple line plot with labels, title and grid
"""
import numpy
import pylab

t = numpy.arange(0.0, 1.0+0.01, 0.01)
s = numpy.cos(2*2*numpy.pi*t)
pylab.plot(t, s)

pylab.xlabel('time (s)')
pylab.ylabel('voltage (mV)')
pylab.title('About as simple as it gets, folks')
pylab.grid(True)
pylab.savefig('/Users/USERNAME/Documents/simple_plot.png', format='png')

pylab.show()

The code above returned the same error regardless of whether the path was set explicitly. I tried the following in addition to the above:

pylab.savefig('simple_plot.png', format='png')
pylab.savefig('simple_plot')

I tried setting the path explicitly as the other question said (as in the longer example above), but that resulted in the following errors. Note: mplex.py is the name of the script.

libpng warning: Application was compiled with png.h from libpng-1.2.44
libpng warning: Application  is  running with png.c from libpng-1.4.11
libpng warning: Incompatible libpng version in application and library
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mplex.py", line 16, in <module>
pylab.savefig('/Users/USERNAME/Documents/simple_plot.png', format='png')
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/pyplot.py", line 474, in savefig
return fig.savefig(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/figure.py", line 1225, in savefig
self.canvas.print_figure(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/backend_bases.py", line 2075, in print_figure
**kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/backend_bases.py", line 1846, in print_png
return agg.print_png(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/backends/backend_agg.py", line 497, in print_png
filename_or_obj, self.figure.dpi)
RuntimeError: Could not create write struct

Ideas? Is there another way to explicitly set the destination aside from what I tried in the savefig line?

Community
  • 1
  • 1
danielle47
  • 51
  • 1
  • 6
  • you have to relink you libraries since the libpng does not match it is not a problem with your path. I dont know where are the libraries where you have to link since i am lazy and I use linux – user1462442 Jul 12 '12 at 23:56
  • I'm new to python - assuming I find the libraries, how would I relink them? – danielle47 Jul 20 '12 at 00:47
  • I dont really know. Sorry. If I did, then I would have posted it as an answer, but I do not use a mac. I dont know where the libraries are. I realize package management is hard. – user1462442 Jul 25 '12 at 17:24
  • How did you install Python 3.2? And matplotlib? Most importantly, did you compile them yourself? If not, where did you get them? It seems as though your matplotlib installation wants a much older libpng version than you have. – cge Jul 31 '12 at 01:40

2 Answers2

5

I'm not sure what package manager you're using, and I'm on Python 2.7 (not 3.2.3), but I'm using Fink and had a similar problem (with different libpng version numbers).

In iPython --pylab, I was seeing this:

In [3]: savefig('foo.png')
libpng warning: Application  is  running with png.c from libpng-1.4.11
libpng warning: Incompatible libpng version in application and library
libpng warning: Application was compiled with png.h from libpng-1.5.11

Checking my Fink installations of libpng, I saw this:

$ fink list libpng
Information about 6268 packages read in 0 seconds.
 i   libpng14             1.4.11-1        PNG image format handling library
 i   libpng14-shlibs      1.4.11-1        Shared libraries for libpng14 package
     libpng15             1.5.10-1        PNG image format handling library
     libpng15-32bit       1.5.10-1        PNG library (32-bit)
     libpng15-32bit-shli  1.5.10-1        Shared libraries for libpng15 package
 i   libpng15-shlibs      1.5.10-1        Shared libraries for libpng15 package
     libpng3              1:1.2.49-1      PNG image format handling library
     libpng3-shlibs       1:1.2.49-1      Shared libraries for libpng3 package
 i   system-pkgconfig-li  1.5.11-1        [virtual pkgconfig package representing libpng]
 i   system-pkgconfig-li  1.5.11-1        [virtual pkgconfig package representing libpng15]

After running "fink install libpng15", my libpng package versions looked like this:

$ fink list libpng
Information about 6268 packages read in 1 seconds.
     libpng14             1.4.11-1        PNG image format handling library
 i   libpng14-shlibs      1.4.11-1        Shared libraries for libpng14 package
 i   libpng15             1.5.10-1        PNG image format handling library
     libpng15-32bit       1.5.10-1        PNG library (32-bit)
     libpng15-32bit-shli  1.5.10-1        Shared libraries for libpng15 package
 i   libpng15-shlibs      1.5.10-1        Shared libraries for libpng15 package
     libpng3              1:1.2.49-1      PNG image format handling library
     libpng3-shlibs       1:1.2.49-1      Shared libraries for libpng3 package
 i   system-pkgconfig-li  1.5.11-1        [virtual pkgconfig package representing libpng]
 i   system-pkgconfig-li  1.5.11-1        [virtual pkgconfig package representing libpng15]

As you can see, libpng14 is no longer installed and libpng15 is installed.

Next, "pip uninstall matplotlib" and "pip install matplotlib" yields a matplotlib build summary like this:

$ pip install matplotlib
Downloading/unpacking matplotlib
  Downloading matplotlib-1.2.0.tar.gz (36.9Mb): 36.9Mb downloaded
  Running setup.py egg_info for package matplotlib
    basedirlist is: ['/usr/local/', '/usr', '/usr/X11', '/opt/local']
    ============================================================================
    BUILDING MATPLOTLIB
                matplotlib: 1.2.0
                    python: 2.7.3 (default, Jul 27 2012, 16:40:57)  [GCC 4.2.1
                            Compatible Apple Clang 3.1
                            (tags/Apple/clang-318.0.61)]
                  platform: darwin

    REQUIRED DEPENDENCIES
                     numpy: 1.6.2
                 freetype2: 15.0.9

    OPTIONAL BACKEND DEPENDENCIES
                    libpng: 1.5.10
                   Tkinter: Tkinter: 81008, Tk: 8.5, Tcl: 8.5
                      Gtk+: no
                            * Building for Gtk+ requires pygtk; you must be able
                            * to "import gtk" in your build/install environment
           Mac OS X native: yes
                        Qt: no
                       Qt4: no
                    PySide: no
                     Cairo: no

    OPTIONAL DATE/TIMEZONE DEPENDENCIES
                  dateutil: 1.5
                      pytz: 2012b

    OPTIONAL USETEX DEPENDENCIES
                    dvipng: no
               ghostscript: 9.05
                     latex: no    

You can see that the build will now pick up libpng 1.5.10. Previously, it was picking up 1.4.11 for the build.

debuggr
  • 51
  • 1
  • 3
-2

Avoid using the standard console and the error will not appear by double clicking the yourcode.py or yourcode.pyw file.

glh
  • 4,900
  • 3
  • 23
  • 40
Eric
  • 1