我正在尝试测试matplotlib是否在python3中正确安装。我正在学习这个基础教程:http://www.scipy.org/Plotting_Tutorial
此问题与one asked previously类似,但遗憾的是没有明确报告最终解决方案,这是一个不同的操作系统。
此操作系统是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行中尝试的内容之外,还有另一种方法可以明确地设置目的地吗?
答案 0 :(得分:5)
我不确定你正在使用什么软件包管理器,而且我使用的是Python 2.7(而不是3.2.3),但我使用Fink并遇到了类似的问题(使用不同的libpng版本号)。< / p>
在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。
答案 1 :(得分:-2)
避免使用标准控制台,双击yourcode.py
或yourcode.pyw
文件不会显示错误。