Python pyplot savefig引发OSError:[Errno 9]错误的文件描述符
当我试图将matplotlib图形保存到文件时,我不断收到错误的文件描述错误。我的代码如下:Python pyplot savefig引发OSError:[Errno 9]错误的文件描述符,python,matplotlib,Python,Matplotlib,当我试图将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]
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
OSError:[Errno 9]错误的文件描述符
上述异常是以下异常的直接原因:
SystemError:返回了一个带有错误集的结果
上述异常是以下异常的直接原因:
SystemError:返回了一个带有错误集的结果
上述异常是以下异常的直接原因:
SystemError:返回了一个带有错误集的结果
上述异常是以下异常的直接原因:
SystemError:返回了一个带有错误集的结果
上述异常是以下异常的直接原因:
SystemError:返回了一个带有错误集的结果
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“C:\ProgramFiles(x86)\Python37-32\lib\site-
packages\matplotlib\cbook\\ uuuuu init\ uuuuu.py”,第410行,在open\ u文件\u cm中
收益率fh
文件“C:\ProgramFiles(x86)\Python37-32\lib\site-
packages\matplotlib\backends\backend\u agg.py”,第523行,打印格式为png
self.figure.dpi,元数据=元数据)
SystemError:返回了一个带有错误集的结果
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\mutas\Documents\tpf01.py”,第8行,在
plt.savefig('c.png')
文件“C:\ProgramFiles(x86)\Python37-32\lib\site-
packages\matplotlib\pyplot.py”,第688行,保存图
res=图savefig(*args,**kwargs)
文件“C:\ProgramFiles(x86)\Python37-32\lib\site-
packages\matplotlib\figure.py”,第2097行,保存图
self.canvas.print_图(fname,**kwargs)
文件“C:\Program Files(x86)\Python37-32\lib\site packages\matplotlib\backend\u base.py”,第2075行,如图所示
**kwargs)
文件“C:\ProgramFiles(x86)\Python37-32\lib\site-
packages\matplotlib\backends\backend\u agg.py”,第523行,打印格式为png
self.figure.dpi,元数据=元数据)
文件“C:\ProgramFiles(x86)\Python37-32\lib\contextlib.py”,第130行,在
__出口__
self.gen.throw(类型、值、回溯)
文件“C:\ProgramFiles(x86)\Python37-32\lib\site-
packages\matplotlib\cbook\\ uuuuu init\ uuuuu.py”,第410行,在open\ u文件\u cm中
收益率fh
OSError:[Errno 9]错误的文件描述符
这里发生了什么?请注意,这不是系统上matplotlib的新安装。我已经用了一段时间了,这个问题突然出现了。重新安装matplotlib没有任何帮助。所有这些
SystemError
s表明CPython中有一个bug!对于不同的文件也会发生这种情况吗?您确定文件已正确关闭吗?@ImportanceOfBeingErnest是的,每个文件。我尝试创建新文件,但结果仍然相同。