【问题标题】:How to generate vertical PDF using pdfpages in python如何在python中使用pdfpages生成垂直PDF
【发布时间】:2015-11-16 18:42:29
【问题描述】:

我正在使用from matplotlib.backends.backend_pdf import PdfPages 生成包含多个(子)图的 PDF。

有没有办法控制生成的 pdf 的方向(水平/垂直)?我经常得到水平 PDF 的

谢谢

【问题讨论】:

    标签: python pdf matplotlib


    【解决方案1】:

    如果您使用set_size_inches 函数设置图形的大小,PDF 应该自动成为您想要的形状:

    from matplotlib.backends.backend_pdf import PdfPages
    import matplotlib.pyplot as plt
    
    with PdfPages('portrait.pdf') as pdf:
        x = range(10)
        y = [y * 2 for y in x]
    
        plt.figure()
        plt.clf()
        plt.plot(x, y)
        plt.xlabel('x axis')
        plt.ylabel('y axis')
        figure = plt.gcf()
        figure.set_size_inches([7,10])
        pdf.savefig(figure)
    

    如果您将大小更改为[10,7],您应该会看到方向自动切换。

    savefig 确实有一个方向设置,例如orientation='portrait' 但我认为这不会有任何影响。不过你可以试试。

    【讨论】:

    【解决方案2】:

    以下内容对我有用:

    with PdfPages('portrait.pdf') as pdf:
         pdf.savefig(fig, orientation='portrait')
         plt.close()
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2018-10-22
    • 2014-01-20
    • 1970-01-01
    • 2019-10-30
    相关资源
    最近更新 更多