[英]Add multiple plots to one PDF page
我正在嘗試將 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()
另一種方法是使用matplotlib.pyplot.subplot
並將所有圖包裝在一個figure
。
這是一個簡單的例子:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.