บันทึกแปลงเป็น PDF


91

โมดูลการวางแผน

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ฟังก์ชันในโมดูลหลักได้

มีฟังก์ชันที่ตั้งชื่อpylab.savefigแต่ดูเหมือนว่าจะใช้งานได้ก็ต่อเมื่อวางไว้พร้อมกับโมดูลการวางแผน มีวิธีอื่นอีกไหมที่จะทำให้สำเร็จ?

คำตอบ:


212

หากมีใครมาจาก 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')

1
คุณกำหนดขนาดหน้าของ pdf ได้อย่างไร?
ที่ไหนบังคับ

2
@wherestheforce ฉันไม่แน่ใจว่าคุณสามารถกำหนดขนาดหน้าของ pdf ได้โดยตรง แต่คุณสามารถเปลี่ยนขนาดรูปได้เช่น f = plt.figure (figsize = (5, 10)) เพื่อเปลี่ยนอัตราส่วน pdf
Clement T.

118

สำหรับหลาย ๆ แปลงในไฟล์ 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()

3
"การจัดวางพล็อต" สมควรเป็นตัวอย่างเพื่ออธิบายวิธีการเพิ่มพล็อตให้กับตัวเลข!
user2127595

1
@ 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) return fig
DeanM

22
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()

3
หากคุณกำลังใช้นำมันหลังจากplt.show() pdf.savefig()
จาก keras import michael

-25

ไม่เป็นไรมีวิธีทำ

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')

----- โมดูลหลัก -----


19
เดี๋ยวก่อนฉันคิดว่าคุณต้องการบันทึกพล็อตเป็นไฟล์ PDF ไฟล์เดียว โซลูชันของคุณจะบันทึกรูปภาพเป็นไฟล์ PNG แยกกันสามไฟล์ซึ่งดูเหมือนว่าจะเป็นคำตอบสำหรับคำถามอื่น
DSM

2
ขออภัยเป็นอย่างยิ่ง ฉันกำลังให้ความสำคัญกับการบันทึกไฟล์มากขึ้น ฉันรู้เกี่ยวกับสิ่งที่แบ็กเอนด์ pdf .. แต่ก็ทำงานของฉันและละเลยที่จะเพิ่มมัน อย่างไรก็ตามขอบคุณที่ชี้ให้เห็น
VoodooChild92

5
เมื่อเห็นจำนวนการโหวตลดลงคุณอาจคิดถึงการลบคำตอบนี้เพื่อออกจาก "ห้อง" สำหรับคำตอบอื่น ๆ
PatrickT
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.