Matplotlib figure to PDF without saving

前端 未结 3 2176
独厮守ぢ
独厮守ぢ 2021-02-11 04:10

I have to create a group of matplotlib figures, which I would like to directly present in a PDF report without saving them as a file.

The data for my plots is stored in

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-11 04:42

    I think you can save the figure into a buffer using io.BytessIO and use that in platypus. Something like this perhaps?

    import io
    import matplotlib.pylab as plt
    from reportlab.platypus import BaseDocTemplate, Image
    
    buffers = []
    
    for index, row in myDataFrame.iterrows():
        fig = plt.figure()
        plt.plot(row['Xvalues'], row['Yvalues'],'o', color='r')
    
        mybuffer = io.BytesIO()
        fig.savefig(mybuffer, format = 'pdf')
        mybuffer.seek(0)
        buffers.append(mybuffer)
        plt.close(fig)
    
    text = []
    doc = BaseDocTemplate(pageName, pagesize=landscape(A4))
    
    doc.build(buffers)
    

提交回复
热议问题