简体   繁体   中英

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.

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 .

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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