【问题标题】:Save multiple plots in a single PDF file将多个绘图保存在一个 PDF 文件中
【发布时间】:2012-07-04 22:48:14
【问题描述】:

绘图模块

def plotGraph(X,Y):
    fignum = random.randint(0,sys.maxint)
    plt.figure(fignum)
    ### Plotting arrangements ###
    return fignum

主模块

import matplotlib.pyplot as plt
### tempDLStats, tempDLlabels are the argument
plot1 = plotGraph(tempDLstats, tempDLlabels)
plot2 = plotGraph(tempDLstats_1, tempDLlabels_1)
plot3 = plotGraph(tempDLstats_2, tempDLlabels_2)
plt.show()

我想将所有图形 plot1、plot2、plot3 保存到一个 PDF 文件中。有没有办法实现它?我不能在主模块中包含plotGraph 函数。

有一个名为pyplot.savefig 的函数,但它似乎只适用于单个数字。有没有其他方法可以实现?

【问题讨论】:

  • 我在这里为基本上相同的问题提供了一个解决方案:stackoverflow.com/questions/38938454/… 仅供参考;它可能很有趣,提供多功能模板代码,其中包含许多演示自定义每页子图的数量、它们之间的间距、整体样式等,使用来自 matplotlib 的 PdfPages 和可选的更高级的代码模板,利用 seaborn

标签: python matplotlib


【解决方案1】:

如果有人从 google 来到这里,希望将单个数字转换为 .pdf(这正是我想要的):

import matplotlib.pyplot as plt

f = plt.figure()
plt.plot(range(10), range(10), "o")
plt.show()

f.savefig("foo.pdf", bbox_inches='tight')

【讨论】:

  • 如何设置pdf的页面大小?
  • @wherestheforce 我不确定您是否可以直接设置 pdf 的页面大小,但您可以更改图形大小:例如 f = plt.figure(figsize=(5, 10)) 来更改pdf比例。
【解决方案2】:
import datetime
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

# Create the PdfPages object to which we will save the pages:
# The with statement makes sure that the PdfPages object is closed properly at
# the end of the block, even if an Exception occurs.
with PdfPages('multipage_pdf.pdf') as pdf:
    plt.figure(figsize=(3, 3))
    plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
    plt.title('Page One')
    pdf.savefig()  # saves the current figure into a pdf page
    plt.close()

    plt.rc('text', usetex=True)
    plt.figure(figsize=(8, 6))
    x = np.arange(0, 5, 0.1)
    plt.plot(x, np.sin(x), 'b-')
    plt.title('Page Two')
    pdf.savefig()
    plt.close()

    plt.rc('text', usetex=False)
    fig = plt.figure(figsize=(4, 5))
    plt.plot(x, x*x, 'ko')
    plt.title('Page Three')
    pdf.savefig(fig)  # or you can pass a Figure object to pdf.savefig
    plt.close()

    # We can also set the file's metadata via the PdfPages object:
    d = pdf.infodict()
    d['Title'] = 'Multipage PDF Example'
    d['Author'] = u'Jouni K. Sepp\xe4nen'
    d['Subject'] = 'How to create a multipage pdf file and set its metadata'
    d['Keywords'] = 'PdfPages multipage keywords author title subject'
    d['CreationDate'] = datetime.datetime(2009, 11, 13)
    d['ModDate'] = datetime.datetime.today()

【讨论】:

  • 如果你使用plt.show(),把它放在pdf.savefig()之后。
【解决方案3】:

对于单个 pdf 文件中的多个绘图,您可以使用 PdfPages

plotGraph 函数中,您应该返回图形,然后调用图形对象的savefig

------绘图模块------

def plotGraph(X,Y):
      fig = plt.figure()
      ### Plotting arrangements ###
      return fig

------绘图模块------

-----主模块----

from matplotlib.backends.backend_pdf import PdfPages

plot1 = plotGraph(tempDLstats, tempDLlabels)
plot2 = plotGraph(tempDLstats_1, tempDLlabels_1)
plot3 = plotGraph(tempDLstats_2, tempDLlabels_2)

pp = PdfPages('foo.pdf')
pp.savefig(plot1)
pp.savefig(plot2)
pp.savefig(plot3)
pp.close()

【讨论】:

  • “Plotting Arrangements”值得一个例子来解释如何实际添加绘图到图形!
  • @user2127595 这对我有用: def plot_graph(x, y1, y2): fig = plt.figure() axes1 = fig.add_subplot(2, 1, 1) axes2 = fig.add_subplot( 2, 1, 2) axes1.plot(x, y1) axes2.plot(x, y2) 返回图
【解决方案4】:

没关系有办法做到这一点。

def plotGraph(X,Y):
     fignum = random.randint(0,sys.maxint)
     fig = plt.figure(fignum)
     ### Plotting arrangements ###
     return fig

------绘图模块------

-----主模块----

 import matplotlib.pyplot as plt
 ### tempDLStats, tempDLlabels are the argument
 plot1 = plotGraph(tempDLstats, tempDLlabels)
 plot2 = plotGraph(tempDLstats_1, tempDLlabels_1)
 plot3 = plotGraph(tempDLstats_2, tempDLlabels_2)
 plt.show()
 plot1.savefig('plot1.png')
 plot2.savefig('plot2.png')
 plot3.savefig('plot3.png')

----- 主模块 -----

【讨论】:

  • 等等,我以为你想将绘图保存到单个 PDF 文件中。您的解决方案将图像保存到三个单独的 PNG 文件中,这似乎是另一个问题的答案。
  • 非常抱歉。我更专注于以某种方式保存文件。我知道后端 pdf 的事情..但是继续我的工作而忽略了添加它。无论如何,感谢您指出这一点。
  • 看到反对票的数量,您可能会考虑删除此答案,以便为其他答案留出“空间”。
猜你喜欢
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 2018-08-20
  • 1970-01-01
相关资源
最近更新 更多