I need to give a margin of 2.5cm to a entire A4 size pdf page. This is what I tried.
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib import gridspec
import matplotlib.pyplot as plt
fig_width_cm = 21 # A4 page
fig_height_cm = 29.7
margin_cm = 2.5
inches_per_cm = 1 / 2.54 # Convert cm to inches
fig_width = fig_width_cm * inches_per_cm # width in inches
fig_height = fig_height_cm * inches_per_cm # height in inches
margin = margin_cm * inches_per_cm
left_margin = margin/fig_width
right_margin = 1-margin/fig_width
top_margin = 1-margin/fig_height
bottom_margin = margin/fig_height
fig_size = [fig_width, fig_height]
with PdfPages(pdffilename) as pdf:
plt.rc('text', usetex=False) # so that LaTeX is not needed when creating a PDF with PdfPages later on
fig = plt.figure()
fig.set_facecolor('#9999ff')
gs = gridspec.GridSpec(51, 21)
gs.update(wspace=0., hspace=0.)
fig.set_size_inches(fig_size)
# external axis
ax0 = fig.add_subplot(gs[:, :])
plt.subplots_adjust(left=margin/fig_width, right=1-margin/fig_width, bottom=margin/fig_height, top=1-margin/fig_height,wspace=0., hspace=0.)
pdf.savefig(bbox_inches='tight',pad = 0)
plt.close()
This is how it looks currently.
The left and the bottom margins are different from the other two measurements.What is it that I am doing wrong?
Update:
As an alternative I placed, the axis of ax1 at the edge of the figure using this ax1.set_position([0, 0.8, 0.2, 0.2])
but while taking a printout I observed that some part of the pdf is not printed.