在matplotlib中创建多页PDF,而不在桌面上绘制图形

2024-04-27 03:19:39 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的示例代码中。。。你知道吗

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运行代码不会导致弹出窗口。你知道吗


Tags: 代码fromimport示例pdfmatplotlibas画布
2条回答

我想你要的是弄清楚这个数字。 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

这个问题并不是由matplotlib或类似的东西引起的,但是仅仅从Spyder内部运行代码就导致了这个问题。直接使用python运行代码不会引起我以前遇到的问题。你知道吗

相关问题 更多 >