首页 > 解决方案 > 在 Python 中将多个图打印到 PDF(每页一个图)

问题描述

matplotlib 的文档中,这应该允许我将多个图打印到 PDF 文件中,每页一个图。

import datetime
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

# Create the PdfPages object to which we will save the pages:
# The with statement makes sure that the PdfPages object is closed properly at
# the end of the block, even if an Exception occurs.
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()

    # if LaTeX is not installed or error caught, change to `usetex=False`
    plt.rc('text', usetex=True)
    plt.figure(figsize=(8, 6))
    x = np.arange(0, 5, 0.1)
    plt.plot(x, np.sin(x), 'b-')
    plt.title('Page Two')
    pdf.attach_note("plot of sin(x)")  # you can add a pdf note to
                                       # attach metadata to a page
    pdf.savefig()
    plt.close()

但是,当我运行它时,我得到了几个FileNotFoundErrors,像这样:FileNotFoundError: [WinError 2] The system cannot find the file specified.

我究竟做错了什么?

我已经尝试过设置usetex=False,但这并不能解决它。

完整的错误将长达几页,我不确定分享它的最佳方式是什么,但它是这样开始的:

在此处输入图像描述

标签: pythonmatplotlib

解决方案


推荐阅读