10

スクリプトの異なる部分で作成された 2 つの図を PdfPages を使用して PDF に保存したいのですが、それらを PDF に追加することはできますか?

例:

fig = plt.figure()
ax = fig_zoom.add_subplot(111)
ax.plot(range(10), range(10), 'b')

with PdfPages(pdffilepath) as pdf:
    pdf.savefig(fig)

fig1 = plt.figure()
ax = fig_zoom.add_subplot(111)
ax.plot(range(10), range(2, 12), 'r')

with PdfPages(pdffilepath) as pdf:
    pdf.savefig(fig1)
4

5 に答える 5

9

すみません、それは下手な質問です。withステートメントを使用しないでください。

fig = plt.figure()
ax = fig_zoom.add_subplot(111)
ax.plot(range(10), range(10), 'b')

# create a PdfPages object
pdf = PdfPages(pdffilepath)

# save plot using savefig() method of pdf object
pdf.savefig(fig)

fig1 = plt.figure()
ax = fig_zoom.add_subplot(111)
ax.plot(range(10), range(2, 12), 'r')

pdf.savefig(fig1)

# remember to close the object to ensure writing multiple plots
pdf.close()
于 2014-12-11T09:18:48.797 に答える
0

データがデータフレームにある場合、このように直接行うことができます

#
with PdfPages(r"C:\Users\ddadi\Documents\multipage_pdf1.pdf","a") as pdf:
    #insert first image
    dataframe1.plot(kind='barh'); plt.axhline(0, color='k')
    plt.title("first page")
    pdf.savefig()
    plt.close()

    #insert second image
    dataframe2.plot(kind='barh'); plt.axhline(0, color='k')
    plt.title("second page")
    pdf.savefig()
    plt.close()
于 2016-08-03T10:01:25.223 に答える