Writing pandas/matplotlib image directly into XLSX file

后端 未结 1 918
难免孤独
难免孤独 2021-01-17 23:27

I am generating plots in pandas/matplotlib and wish to write them to an XLSX file. I am not looking to create native Excel charts; I am merely writing the plots as non-inter

相关标签:
1条回答
  • 2021-01-18 00:06

    You can save the image to memory as a file object (not to disk) and then use that when inserting to Excel file:

    import matplotlib.pyplot as plt
    from cStringIO import StringIO
    imgdata = StringIO()
    
    fig, ax = plt.subplots()
    
    # Make your plot here referencing ax created before
    results.resid.hist(ax=ax)
    
    fig.savefig(imgdata)
    
    worksheet.insert_image(row, 0, imgdata)
    
    0 讨论(0)
提交回复
热议问题