Matplotlib figure to PDF without saving

前端 未结 3 2168
独厮守ぢ
独厮守ぢ 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:56

    Here is the best solution provided by matplotlib itself:

    from matplotlib.backends.backend_pdf import PdfPages
    import matplotlib.pyplot as plt
    
    with PdfPages('foo.pdf') as pdf:
        #As many times as you like, create a figure fig and save it:
        fig = plt.figure()
        pdf.savefig(fig)
    
        ....
        fig = plt.figure()
        pdf.savefig(fig) 
    

    Voilà

    Find a full example here: multipage pdf matplotlib

提交回复
热议问题