简体   繁体   English

将多个绘图添加到一个 PDF 页面

[英]Add multiple plots to one PDF page

I am trying to save 3 plots to one page in a PDF file, so far I managed to save them to the same PDF but on 3 different pages.我正在尝试将 3 个绘图保存到 PDF 文件中的一页,到目前为止,我设法将它们保存到同一个 PDF 但在 3 个不同的页面上。

with PdfPages("NY.pdf") as pdf:

    for title, clf in zip(titles_options, classifier):
       clf.fit(train, d_train)
       disp = plot_confusion_matrix(clf, test, d_test,
                                    cmap=plt.cm.Blues,
                                    normalize='true')

       disp.ax_.set_title(title + " person: " + filename)
       print(title)
       print(disp.confusion_matrix)

       pdf.savefig()
   #plt.show()

An alternative is to use matplotlib.pyplot.subplot and wrap all plots in one figure .另一种方法是使用matplotlib.pyplot.subplot并将所有图包装在一个figure

Here is a simple illustration:这是一个简单的例子:

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

# Create dummy data
x = np.arange(1, 10, 0.01)
y = [x ** 2, 1 / x, 1 / x ** 2]


with PdfPages('multipage_pdf.pdf') as pdf:
    fig, axs = plt.subplots(3)
    fig.suptitle('Vertically stacked subplots')
    # Iterate axes
    for i in range(len(axs)):
        axs[i].plot(x, y[i])

    pdf.savefig()  # saves the current figure into a pdf page
    plt.close()

output output

在此处输入图像描述

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

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