首页 > 解决方案 > 在matplotlib中创建多页PDF而不在桌面上绘制图形

问题描述

在以下示例代码中...

from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

with PdfPages('multipage_pdf.pdf') as pdf:
    for i in range(10):
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.scatter([0, 1, 2], [0, 1, 2])
        pdf.savefig()
        plt.close()

...每一个情节都会弹出一个带有实际画布的窗口。是否有一种优雅的解决方案可以跳过屏幕上画布的实际绘制并将绘图直接绘制到多页 pdf 中?

PS:问题仅在 spyder 中运行代码时引起,因此与 spyder 相关,与其他任何内容无关。直接使用 python 运行代码不会导致弹出窗口。

标签: pythonmatplotlibspyderpdfpages

解决方案


我认为您正在寻找的是清除数字。 first_page = plt.figure(figsize=(11.69, 8.27)) first_page.clf() 查看文档: https ://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.clf.html

一些例子: https ://www.programcreek.com/python/example/102298/matplotlib.pyplot.clf


推荐阅读