1

I want to save multiple (6) plots on one page of a pdf file using python. Currently, I'm saving every plot on a new page.

pdf = matplotlib.backends.backend_pdf.PdfPages("output.pdf")
for fig in plots:
    pdf.savefig(plots[fig])
pdf.close()

The result I want to get is, that the first 6 plots are on page 1, the next 6 on page 2, and so on. Does somebody have an idea how to realize this?

Thank you in advance!

luisa
  • 72
  • 8

1 Answers1

1

To save multiple plots on one page of pdf you can use subplot like this:

from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import numpy as np
import math
  
# Get the angles from 0 to 2 pie (360 degree) in narray object
X = np.arange(0, math.pi*2, 0.05)
  
# Using built-in trigonometric function we can directly plot
# the given cosine wave for the given angles
Y1 = np.sin(X)
Y2 = np.cos(X)
Y3 = np.tan(X)
Y4 = np.tanh(X)
  
# Initialise the subplot function using number of rows and columns
figure, axis = plt.subplots(2, 2)
  
# For Sine Function
axis[0, 0].plot(X, Y1)
axis[0, 0].set_title("Sine Function")
  
# For Cosine Function
axis[0, 1].plot(X, Y2)
axis[0, 1].set_title("Cosine Function")
  
# For Tangent Function
axis[1, 0].plot(X, Y3)
axis[1, 0].set_title("Tangent Function")
  
# For Tanh Function
axis[1, 1].plot(X, Y4)
axis[1, 1].set_title("Tanh Function")


pp = PdfPages('onPage.pdf')
plt.savefig(pp, format='pdf')
pp.close()

output:

enter image description here

I'mahdi
  • 23,382
  • 5
  • 22
  • 30