簡體   English   中英

使用matplotlib在pdf上繪制多個圖

[英]Multiple plots on pdf with matplotlib

我已經和pyplot戰斗了幾天了。 我想返回每頁包含4個樣本的pdf報告。 每個都有4個內聯子圖:帶有名稱和一些統計信息的文本,以及3個值隨時間變化的圖形。 我在網上找到了一個教程並進行了嘗試(請參閱下文),但沒有任何幫助。 pdf為空。 我找不到哪里出了問題。 先感謝您 !

from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt 
t=[n*5 for n in range(len(ratio))]

y_list_ratio=[[x*100/l[3]for x in l[2]]for l in hit_ratio]
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)


 pp = PdfPages('multipage.pdf')



# Generate the pages
nb_plots = len(hit_ratio)
nb_plots_per_page = 4
nb_pages = int(numpy.ceil(nb_plots / float(nb_plots_per_page)))
grid_size = (nb_plots_per_page, 4)

for i, elt in enumerate(hit_ratio):
  # Create a figure instance (ie. a new page) if needed
    if i % nb_plots_per_page == 0:
        plt = plt.figure(figsize=(8.27, 11.69), dpi=100)

  # Plot stuffs !
    plt.subplot2grid(grid_size, (i % nb_plots_per_page, 0))

    plt.text(0.5,0.5,"Puit       Hauteur_pic       Digitonine\n"+ \
            str(elt[-1])+"   "+str(elt[5])+"   "+str(elt[6]),horizontalalignment='center',verticalalignment='center', bbox=props)

    plt.subplot2grid(grid_size, (i % nb_plots_per_page, 1))
    plt.plot(t,hit_norm[i][0])

    plt.subplot2grid(grid_size, (i % nb_plots_per_page, 2))
    plt.plot(t,y_list_ratio[i])

    plt.subplot2grid(grid_size, (i % nb_plots_per_page, 3))
    plt.plot(t,elt[7])
    plt.plot(t,elt[8])

  # Close the page if needed
    if (i + 1) % nb_plots_per_page == 0 or (i + 1) == nb_plots:
        fig2.tight_layout()
        pp.savefig(fig2)

# Write the PDF document to the disk
pp.close()

由於您還沒有任何答案,因此我有另一種建議:

嘗試ReportLab。

from reportlab.lib import colors, utils
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, landscape
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Image, PageBreak, KeepTogether
from reportlab.lib.styles import ParagraphStyle as PS
from reportlab.lib.enums import TA_CENTER
from reportlab.platypus.paragraph import Paragraph

landscape_pic_width = 8
letter_pic_width = 6

....

def get_image(path, width=1*inch):
#'''returns an image for adding to report'''
    img = utils.ImageReader(path)
    iw, ih = img.getSize()
    aspect = ih / float(iw)
    return Image(path, width=width, height=(width * aspect))

def plot_stuff():
    #whatever you want to plot, finish with saving the figure

elements = [] #this is a list that will contain the items to be included in the report
report_title = str(report_title) 

c_table = Table(Conditions_Table_data)
c_table.setStyle(TableStyle([('ALIGN', (0,0),(-1,-1),'CENTER'), ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), ('BOX', (0,0),(-1,-1), 2, colors.blueviolet), ('SIZE', (0,0),(-1,-1), 10), ('SPAN',(-3,3),(-1,3))]))
#tells it how to format the table 

#puts in the logo, the assigned report title, the entered report title, the conditions table, and the setup picture
elements.append(get_image(logo_picture, width = 9*inch))
elements.append(Paragraph(document_title, PS(name='Heading1', spaceAfter = 22, fontName = 'Times-Bold', fontSize = 18, alignment = TA_CENTER)))  
#you can append items to the list "elements" with a for loop. 

doc = SimpleDocTemplate(path_plus_title + ".pdf", pagesize=landscape(letter))

#creates the report. Will throw an error if the report exists and is already open. Otherwise, will generate a report 
#this WILL overwrite an existing report with the same name. Timestamps being forced into the data file names help. 
doc.build(elements)

此代碼中肯定缺少某些部分...但是這些是我導入的項目(例如,“ inch”是用於將尺寸乘以該尺寸所需的英寸數的值)

基本上,您將按照進入清單的順序來構建進入報告pdf的清單。對於每個元素,都會進行一些樣式設置。 您可以包括文本(段落),表格(這是“列表列表”,每個列表為一行)和圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM