Python matplotlib pdf savefig提前退出

Python matplotlib pdf savefig提前退出,python,matplotlib,Python,Matplotlib,因此,我复制了给出的示例,当我运行它时,我得到: Traceback (most recent call last): File "C:\Users\User\Documents\Project work\pdf.py", line 9, in <module> with PdfPages('multipage_pdf.pdf') as pdf: AttributeError: __exit__ 回溯(最近一次呼叫最后一次): 文件“C:\Users\User\Docu

因此,我复制了给出的示例,当我运行它时,我得到:

Traceback (most recent call last):
  File "C:\Users\User\Documents\Project work\pdf.py", line 9, in <module>
    with PdfPages('multipage_pdf.pdf') as pdf:
AttributeError: __exit__
回溯(最近一次呼叫最后一次):
文件“C:\Users\User\Documents\Project work\pdf.py”,第9行,在
使用pdf格式的PdfPages('multipage_pdf.pdf'):
AttributeError:\uuu退出__
那我该怎么办?
谢谢

PdfPages
仅在1.3.1版中才成为上下文管理器。看

尤其要注意以下几行:

添加了用于创建多页pdf的上下文管理器(请参见
matplotlib.backends.backend\u pdf.PdfPages


除了公认的答案,我还必须这样做:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

另外,还要避免使用调用显示的
plt.figure()

我在matplotlib 1.2.1中遇到了相同的错误,可能是1.3.1 onlyOk的文档,刚刚检查过。很明显,我不久前因为某种原因降级了。是我的错。Thanks@Lucidnonsense:如果这解决了您的问题,则这是常见的。