1

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.

enter image description here

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.

Jasmine
  • 476
  • 3
  • 22

2 Answers2

1

This what I did.

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
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')
    fig.set_size_inches(fig_size)
    left_margin  = margin / fig_width
    right_margin = margin / fig_width
    bottom_margin = margin / fig_height
    top_margin = margin / fig_height
    x = left_margin    # horiz. position of bottom-left corner
    y = bottom_margin  # vert. position of bottom-left corner
    w = 1 - (left_margin + right_margin) # width of ax
    h = 1 - (bottom_margin + top_margin) # height of axes

    ax = fig.add_axes([x, y, w, h])
    plt.axis('off')

    pdf.savefig(orientation='landscape', papertype='a4',pad = 0)
    plt.close()

It creates a pdf of A4 size and uniform margin of 2.5cm on all the sides.

Jasmine
  • 476
  • 3
  • 22
0

Don't use bbox_inches='tight', it will reduce the size of your figure to a tight layout, also see this answer, for example.

With the tight bbox turned on, your PDF actually becomes 17.6 x 25.94 cm, without it: 21.01 x 29.7 cm.

Update:

Regarding the page size: My PDF viewer (Preview.app on macOS) has an "info" panel which shows the PDF page dimensions. I am not running Ubuntu right now, but I am sure you could check it via the command line.

To show how the resulting margins work, I've included the PDF as a full-page background within a DIN A4 LaTeX document, adding a dashed, red rectangle to show the 2.5cm margin (note how the ticks are outside of the desired margin):

page layout demonstration

Asmus
  • 5,117
  • 1
  • 16
  • 21