How to export Matplotlib graphics with Reportlab?

0

I'm creating a graphical interface, with python and the tkinter module, the interface has to generate a graph and if I click on the export button I have to generate it in a pdf with the Reportlab library. How do I export it?

import matplotlib, sys
 matplotlib.use('TkAgg')
from numpy import arange, sin, pi, cos
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, 
NavigationToolbar2TkAgg
import matplotlib as pl
from matplotlib.figure import Figure
import tkinter as tk
import numpy as np


master = tk.Tk()
master.title("Exportadora")
master.geometry("800x700")


cv = tk.Frame(master, width=400, height=400)
cv.grid(row=0,column=1)

def graficar():
        grafica= fig.gca()
        t = arange(0.0,3.0,0.01)
        Fun1 = sin(2*pi*t)
        grafica.plot(t,Fun1)
        dataPlot.draw()

btnGrafica= tk.Button(master, text='graficar', command=graficar)
btnGrafica.grid(row=1,column=0)

btnExportar= tk.Button(master, text='Exportar')
btnExportar.grid(row=1,column=1)


fig = Figure(figsize=(6,6), dpi=100)
dataPlot = FigureCanvasTkAgg(fig, master=cv)
dataPlot.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
master.mainloop()
    
asked by Leonel Galicia 22.05.2018 в 19:10
source

1 answer

1

The simplest option is to generate an image of the graph using matplotlib.pytplot.savefig and later you just have to proceed as with any image with reportlab.lib.utils.ImageReader and canvas.drawImage :

import io
import sys
import tkinter as tk
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import matplotlib as pl
from matplotlib.figure import Figure
import numpy as np
from reportlab.pdfgen import canvas
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader       


master = tk.Tk()
master.title("Exportadora")
master.geometry("800x700")


cv = tk.Frame(master, width=400, height=400)
cv.grid(row=0,column=1)
fig = Figure(figsize=(6, 6), dpi=100)


def graficar():
    grafica= fig.gca()
    t = np.arange(0.0, 3.0, 0.01)
    Fun1 = np.sin(2 * np.pi * t)
    grafica.plot(t, Fun1)
    dataPlot.draw()

def exportar():
    data = io.BytesIO()
    fig.savefig(data, format='png')
    data.seek(0)

    c = canvas.Canvas("reporte.pdf")
    Image = ImageReader(data)
    c.drawImage(Image, 45, 171, width=500, height=500)
    c.save()

btnGrafica= tk.Button(master, text='graficar', command=graficar)
btnGrafica.grid(row=1,column=0)

btnExportar= tk.Button(master, text='Exportar', command=exportar)
btnExportar.grid(row=1,column=1)


dataPlot = FigureCanvasTkAgg(fig, master=cv)
dataPlot.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
master.mainloop()

This is just a basic example based on your code, it creates a pdf in the working directory of your script called reporte.pdf with default page size (595.27 x 841.89 points, A4 ) with the image of the graph (500 x 500 pixels) approximately centered.

To avoid creating the image on disk, use io.BytesIO to simulate a file in temporary binary mode.

  

ReportLab documentation: link

    
answered by 22.05.2018 / 22:52
source