使用matplotlib保存python plot在windows下失败

时间:2014-12-08 08:10:47

标签: python matplotlib

我想保存一个python情节。使用MacOS,一切运行顺利但在Windows7下失败。

python-command看起来像这样:

savefig('C:\Users\xyz\AppData\Local\Temp\pyplot1468046843481608342.png')

错误消息就是这个:

Traceback (most recent call last):
File "C:\Users\xyz\AppData\Local\Temp\analyze3744796441786382480.py", line 340, in <module>
savefig('C:\Users\xyz\AppData\Local\Temp\pyplot1468046843481608342.png')
File "C:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 577, in savefig
res = fig.savefig(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\figure.py", line 1470, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 2194, in print_figure
**kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 526, in print_png
filename_or_obj = open(filename_or_obj, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:\\Users\xyz\\AppData\\Local\\Temp\\pyplot1468046843481608342.png'

这是我正在使用的Python:

Python 2.7.8 :: Anaconda 2.1.0 (32-bit)

有人可以解释问题的原因吗?

1 个答案:

答案 0 :(得分:2)

我认为您必须在目录之前添加r以将字符串转换为原始字符串。看这篇文章

Unknown python expression filename=r'/path/to/file'