当代码很长时,我发现 matplotlib 示例不方便。它的“with”块必须包含整个代码。打印多个图时有什么方法可以避免它?
with PdfPages('multipage_pdf.pdf') as pdf:
plt.figure(figsize=(3, 3))
plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
plt.title('Page One')
pdf.savefig() # saves the current figure into a pdf page
plt.close()
我在这里找到了答案。 只需创建 pdf 对象,在每个图上调用 savefig 并在完成后关闭。