Matplotlib est très intéressant pour faire des graph, il fait un peu de PDF mais ça me convient pas. Reportlab est très bien pour faire des PDF.
J' essaie de faire un PDF et d'y inclure mon graph. Mais Reportlab accepte du JPEG là où Matplotlib donne du PNG.
J' ai essayé d' avoir une conversion dans la mémoire sans passer par un fichier sur le disque dur. Je suis peu habitué à utiliser le module io. J'ai pompé du code sur S.O. que j'ai traffiqué mais ça marche pas :
#!/usr/bin/python3
#-*- coding: utf-8 -*-
# How to use reportlab.platypus.Image with PNG that is created by matplotlib.pyplot.save_figure function :
# https://gist.github.com/kokardy/5967661
# http://stackoverflow.com/questions/8598673/how-to-save-a-pylab-figure-into-in-memory-file-which-can-be-read-into-pil-image
import io
from io import StringIO, BytesIO
from PIL import ImageFileIO
import matplotlib.pyplot as plt
from reportlab.platypus import Image
from PIL import Image
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
#plot your graphs *********************************----------------------
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs', 'Gogs'
sizes = [15, 30, 45, 5, 5]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral', 'magenta']
explode = (0, 0.1, 0, 0, 0.9) # only "explode" the 2nd slice (i.e. 'Hogs')
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90)
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
buffer = io.BytesIO()
plt.savefig(buffer, format='png')
buffer.seek(0)
im = Image.open(buffer)
im.show()
print(buffer)
print("\n")
print(im)
def hello(c):
# move the origin up and to the left
c.translate(inch,inch)
# define a large font
c.setFont("Helvetica", 14)
# choose some colors
c.setStrokeColorRGB(0.2,0.5,0.3)
c.setFillColorRGB(1,0,1)
# draw some lines
c.line(0,0,0,1.7*inch)
c.line(0,0,1*inch,0)
# draw a rectangle
c.rect(0.2*inch,0.2*inch,1*inch,1.5*inch, fill=1)
# make text go straight up
c.rotate(90)
# change color
c.setFillColorRGB(0,0,0.77)
# say hello (note after rotate the y coord needs to be negative!)
c.drawString(0.3*inch, -inch, "Hello World")
c.drawImage(buffer,50,75, width=2*inch, height=2*inch)
c = canvas.Canvas("hello.pdf")
# hello(c)
# c.showPage()
c.save()
buffer.close()