简体   繁体   English

如何在python中使用pdfpages生成垂直PDF

[英]How to generate vertical PDF using pdfpages in python

I am using from matplotlib.backends.backend_pdf import PdfPages to generate PDFs containing multiple (sub)plots. 我正在使用from matplotlib.backends.backend_pdf import PdfPages生成包含多个(子)图的PDF。

Is there a way to control the orientation (horizontal/vertical) of the resulting pdf? 有没有一种方法可以控制生成的pdf的方向(水平/垂直)? I constantly get horizontal PDF's 我不断得到水平PDF

thanks 谢谢

If you set the size of your figure using the set_size_inches function, the PDF should automatically be the shape you want: 如果使用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)

If you change the size to [10,7] , you should see the orientation automatically switch. 如果将大小更改为[10,7] ,应该会看到方向自动切换。

savefig does have an orientation setting, eg orientation='portrait' but I don't think this will have any effect. savefig确实具有方向设置,例如, savefig orientation='portrait'但我认为这不会产生任何效果。 You could try it though. 您可以尝试一下。

The following worked for me: 以下为我工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM