【问题标题】:Save multiple plots on a single pdf-page using python使用 python 在单个 pdf 页面上保存多个图
【发布时间】:2021-10-16 12:16:54
【问题描述】:

我想使用 python 在 pdf 文件的一页上保存多 (6) 个图。 目前,我正在将每个情节都保存在新页面上。

pdf = matplotlib.backends.backend_pdf.PdfPages("output.pdf")
for fig in plots:
    pdf.savefig(plots[fig])
pdf.close()

我想要得到的结果是,前 6 个图在第 1 页,接下来的 6 个在第 2 页,依此类推。有人知道如何实现这一点吗?

提前谢谢你!

【问题讨论】:

    标签: python matplotlib pdf figure savefig


    【解决方案1】:

    要在 一页 pdf 上保存多个绘图,您可以像这样使用subplot

    from matplotlib.backends.backend_pdf import PdfPages
    import matplotlib.pyplot as plt
    import numpy as np
    import math
      
    # Get the angles from 0 to 2 pie (360 degree) in narray object
    X = np.arange(0, math.pi*2, 0.05)
      
    # Using built-in trigonometric function we can directly plot
    # the given cosine wave for the given angles
    Y1 = np.sin(X)
    Y2 = np.cos(X)
    Y3 = np.tan(X)
    Y4 = np.tanh(X)
      
    # Initialise the subplot function using number of rows and columns
    figure, axis = plt.subplots(2, 2)
      
    # For Sine Function
    axis[0, 0].plot(X, Y1)
    axis[0, 0].set_title("Sine Function")
      
    # For Cosine Function
    axis[0, 1].plot(X, Y2)
    axis[0, 1].set_title("Cosine Function")
      
    # For Tangent Function
    axis[1, 0].plot(X, Y3)
    axis[1, 0].set_title("Tangent Function")
      
    # For Tanh Function
    axis[1, 1].plot(X, Y4)
    axis[1, 1].set_title("Tanh Function")
    
    
    pp = PdfPages('onPage.pdf')
    plt.savefig(pp, format='pdf')
    pp.close()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2021-11-14
      • 1970-01-01
      • 2022-01-01
      • 2014-01-03
      相关资源
      最近更新 更多