1

我想要 (8,6) 或任何其他自定义大小的图,具体取决于 A4 或字母大小 pdf 上的数据类型。我编写了附加代码,我在其中设置了图形大小plt.subplots(figsize=(width, height))。我可以设置plt.subplots(figsize=(8.27 x 11.69))pdf A4 但数字变大是正常的。我想要的地块正好是 (8,6) 并且在 A4 尺寸纸的中间。我尝试过pdf.savefig(),但该关键字papertype='a4'仅适用于 postscript 输出。让我知道如何在 A4、A3 或任何大小的 pdf 上导出绘图,而不管绘图/无花果大小如何,确保无花果大小小于 pdf 纸张大小。我非常感谢任何技术。

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()
4

0 回答 0