如何避免从 python 数据帧生成多页 pdf 中的“with”块

问题描述 投票:0回答:1

当代码很长时,我发现 matplotlib 示例不方便。它的“with”块必须包含整个代码。打印多个图时有什么方法可以避免它?

with PdfPages('multipage_pdf.pdf') as pdf:
    plt.figure(figsize=(3, 3))
    plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
    plt.title('Page One')
    pdf.savefig()  # saves the current figure into a pdf page
    plt.close()
python dataframe matplotlib
1个回答
0
投票

我在这里找到了答案。 只需创建 pdf 对象,在每个图上调用 savefig 并在完成后关闭。

© www.soinside.com 2019 - 2024. All rights reserved.