1

我正在尝试测试 matplotlib 是否在 python3 中正确安装。我正在关注这个基本教程:http ://www.scipy.org/Plotting_Tutorial

这个问题与之前提出的问题类似,但遗憾的是没有明确报告最终解决方案,这是一个不同的操作系统。

用于此的操作系统是 Mac OS X 10.6.8。在 Python 3.2 中运行脚本不起作用,但在 Python 2.7 中运行它。不幸的是我需要3.2。

当 savefig 行被注释掉时,它工作正常。

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

无论是否明确设置了路径,上面的代码都会返回相同的错误。除了上述之外,我还尝试了以下方法:

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

我尝试像另一个问题所说的那样明确设置路径(如上面更长的示例),但这导致了以下错误。注意:mplex.py 是脚本的名称。

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

想法?除了我在 savefig 行中尝试的方法之外,还有另一种方法可以明确设置目标吗?

4

2 回答 2

5

我不确定您使用的是什么包管理器,而且我使用的是 Python 2.7(不是 3.2.3),但我使用的是 Fink 并且遇到了类似的问题(使用不同的 libpng 版本号)。

在 iPython --pylab 中,我看到了这个:

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

检查我的 libpng 的 Fink 安装,我看到了这个:

$ 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]

运行“fink install libpng15”后,我的 libpng 包版本如下所示:

$ 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]

如您所见,libpng14 不再安装,而 libpng15 已安装。

接下来,“pip uninstall matplotlib”和“pip install matplotlib”会生成一个 matplotlib 构建摘要,如下所示:

$ 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    

您可以看到构建现在将选择 libpng 1.5.10。以前,它为构建选择了 1.4.11。

于 2012-11-12T20:16:43.807 回答
-2

避免使用标准控制台,双击yourcode.pyyourcode.pyw文件不会出现错误。

于 2013-01-12T08:53:09.990 回答