0

I've been fighting with pyplot for few days now. I want to return a pdf report with 4 samples on each page. 4 inline subplots for each: text with the name and some statistics, and 3 graphs of values vs time. I found a tutorial online and tried it (see below) but it gives nothing. the pdf is empty. I can't find where it is wrong. Thank you in advance !

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()
8
  • 1
    Can you complete the script, so we can run it and check if it works? Commented Aug 5, 2015 at 13:33
  • Hmm, it is actually working on a result list obtained after many lines of code Commented Aug 5, 2015 at 13:40
  • You have not included numpy, Matplotlib, pdfpages.... Commented Aug 5, 2015 at 13:45
  • Ah yeah but I did on the actual code Commented Aug 5, 2015 at 13:58
  • 1
    We can't guess what your problem is with a [minimal working example] (en.m.wikipedia.org/wiki/Minimal_Working_Example). You don't need to use your real data, you can just plot four straight lines, it does not matter. Commented Aug 6, 2015 at 13:03

1 Answer 1

1

Since you don't have any answers yet, I have an alternate suggestion:

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

There's definitely sections missing from this code...but these are the items I import ("inch", for instance, is a value for sizing that you multiply by the number of inches you want for that dimension)

You basically build a list of the items that go into your report pdf in the order they go in. For each element, there's some style setting that takes place. You can include text (Paragraphs), tables (it's a "list of lists" with each list being a row), and images.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the nice library I didn't know, but i'm getting close to success with my code and I have not enough time to dive in a brand new stuff! But this may prove usefull later thanks ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.