Can matplotlib add metadata to saved figures?

后端 未结 4 900
青春惊慌失措
青春惊慌失措 2021-01-31 16:53

I want to be able to ascertain the provenance of the figures I create using matplotlib, i.e. to know which version of my code and data created these figures. (See this essay for

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-31 17:15

    If you are interested in PDF files, then you can have a look at the matplotlib module matplotlib.backends.backend_pdf. At this link there is a nice example of its usage, which could be "condensed" into the following:

    import pylab as pl
    import numpy as np
    from matplotlib.backends.backend_pdf import PdfPages
    
    pdffig = PdfPages('figure.pdf')
    
    x=np.arange(10)
    
    pl.plot(x)
    pl.savefig(pdffig, format="pdf")
    
    metadata = pdffig.infodict()
    metadata['Title'] = 'Example'
    metadata['Author'] = 'Pluto'
    metadata['Subject'] = 'How to add metadata to a PDF file within matplotlib'
    metadata['Keywords'] = 'PdfPages example'
    
    pdffig.close()
    

提交回复
热议问题