简体   繁体   English

matplotlib pdf savefig提前退出

[英]matplotlib pdf savefig exiting early

So I've copied the example given here and when I run it I get: 所以我复制了这里给出的示例,运行时我得到:

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__

So where do I go from here? 那我从这里去哪里呢? Thanks 谢谢

PdfPages has become a context manager only in version 1.3.1. PdfPages仅在版本1.3.1中才成为上下文管理器。 See the chagelog . 参见chagelog

In particular, observe the following line: 特别要注意以下几行:

Added a context manager for creating multi-page pdfs (see matplotlib.backends.backend_pdf.PdfPages ). 添加了用于创建多页pdf的上下文管理器(请参见matplotlib.backends.backend_pdf.PdfPages )。

In addition to the accepted answer, I had to do this: 除了可接受的答案,我还必须这样做:

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

And also avoid using plt.figure() which invokes the display. 还要避免使用调用显示的plt.figure()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM