我正在尝试测试 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 行中尝试的方法之外,还有另一种方法可以明确设置目标吗?