当我尝试将matplotlib图形保存到文件时,我不断收到错误的文件描述错误。我的代码如下:
import os
os.chdir('C:\\users\\mutas\\documents\\')
import matplotlib.pyplot as plt
plt.text(0.5,0.5, 'some text')
if os.path.isfile('c.png'):
plt.savefig('c.png')
运行时,会引发以下错误:
OSError: [Errno 9] Bad file descriptor
The above exception was the direct cause of the following exception:
SystemError: <built-in method write of _io.BufferedWriter object at
0x0C7E0360> returned a result with an error set
The above exception was the direct cause of the following exception:
SystemError: <built-in method write of _io.BufferedWriter object at
0x0C7E0360> returned a result with an error set
The above exception was the direct cause of the following exception:
SystemError: <built-in method write of _io.BufferedWriter object at
0x0C7E0360> returned a result with an error set
The above exception was the direct cause of the following exception:
SystemError: <built-in method write of _io.BufferedWriter object at
0x0C7E0360> returned a result with an error set
The above exception was the direct cause of the following exception:
SystemError: <built-in method write of _io.BufferedWriter object at
0x0C7E0360> returned a result with an error set
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python37-32\lib\site-
packages\matplotlib\cbook\__init__.py", line 410, in open_file_cm
yield fh
File "C:\Program Files (x86)\Python37-32\lib\site-
packages\matplotlib\backends\backend_agg.py", line 523, in print_png
self.figure.dpi, metadata=metadata)
SystemError: <built-in method write of _io.BufferedWriter object at
0x0C7E0360> returned a result with an error set
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\mutas\Documents\tpf01.py", line 8, in <module>
plt.savefig('c.png')
File "C:\Program Files (x86)\Python37-32\lib\site-
packages\matplotlib\pyplot.py", line 688, in savefig
res = fig.savefig(*args, **kwargs)
File "C:\Program Files (x86)\Python37-32\lib\site-
packages\matplotlib\figure.py", line 2097, in savefig
self.canvas.print_figure(fname, **kwargs)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\matplotlib\backend_bases.py", line 2075, in print_figure
**kwargs)
File "C:\Program Files (x86)\Python37-32\lib\site-
packages\matplotlib\backends\backend_agg.py", line 523, in print_png
self.figure.dpi, metadata=metadata)
File "C:\Program Files (x86)\Python37-32\lib\contextlib.py", line 130, in
__exit__
self.gen.throw(type, value, traceback)
File "C:\Program Files (x86)\Python37-32\lib\site-
packages\matplotlib\cbook\__init__.py", line 410, in open_file_cm
yield fh
OSError: [Errno 9] Bad file descriptor
这是怎么回事?请注意,这不是系统上新安装的matplotlib。我已经使用了一段时间了,这个问题突然开始发生。