plots in A4 size pdf

909 Views Asked by At

I want (8,6) or any other customized size plots depending on datatype on A4 or letter size pdf. I wrote the attached code where I set the figure size inside plt.subplots(figsize=(width, height)). I can set plt.subplots(figsize=(8.27 x 11.69)) which makes the pdf A4 but the figures are becoming larger which is normal. I want the plots exactly (8,6) and in the middle of the A4 size paper. I tried pdf.savefig() but the keyword papertype='a4' works only with postscript output. Let me know how to export the plots on A4, A3 or whatever size pdfs irrespective of plot/fig size ensuring that the fig size is smaller than the pdf paper size. I highly appreciate any technique.

import pandas as pd
import matplotlib.pyplot as plt
from IPython.core.display import Image, display
from matplotlib.backends.backend_pdf import PdfPages
from natsort import os_sorted
import os
import glob
import re

# use glob to get all the csv files
# in the folder
path = r'D:\LD25\VMT processed\Donaldson_Point_20210120\excel data'  # January adcp obs data
#path = r'D:\LD25\VMT processed\Donaldson_Point_20210308\excel data' # March adcp obs data

obs_files = glob.glob(os.path.join(path, "*.xlsx"))
obs_files = os_sorted(obs_files)
sim = pd.read_excel(r'D:\LD25\ADH simulated\adh_simulated_Jan.xlsx','Sheet1') # January sim data 
#sim = pd.read_excel(r'D:\LD25\ADH simulated\adh_simulated_March.xlsx','Sheet1') # March sim data 

pdf = PdfPages(r'C:\Users\clhal\.spyder-py3\python adcp calibration\pdf\Angle_.pdf')
for i,f in enumerate(obs_files):    # i=count, f=value/name
    fig, ax = plt.subplots(figsize=(8, 6))
    plt.rcParams.update({'font.size': 12})
    # read the csv file
    obs = pd.read_excel(f,'Smoothed_Planview')
    list(sim)
    plt.plot(obs['Distance, in meters'],obs['Velocity direction, in deg. from true north'])
    #plt.plot(sim.iloc[:,2*i],sim.iloc[:,2*i+1])
    ax.set_title('Jan, Transect %d' %(i+1))
    #ax.set_title('March, Transect %d' %(i+1))

    plt.xlabel("Distance (m/s)")
    plt.ylabel(" Direction (degree)")
    # print the location and filename
    print('Location:', f)
    print('File Name:', f.split("\\")[-1])

    # print the content
    print('Content:')
    display(sim)
    print()
    pdf.savefig(fig,bbox_inches='tight',pad_inches=.8)
    plt.savefig(r"C:\Users\clhal\.spyder-py3\python adcp calibration\pdf\tran_{0}.png".format(i+1),dpi=300)
pdf.close()
0

There are 0 best solutions below