Generating a PNG with matplotlib when DISPLAY is undefined

前端 未结 12 1246
感动是毒
感动是毒 2020-11-22 02:51

I am trying to use networkx with Python. When I run this program it get this error. Is there anything missing?

#!/usr/bin/env python

import networkx as nx
i         


        
12条回答
  •  长情又很酷
    2020-11-22 03:21

    For Google Cloud Machine Learning Engine:

    import matplotlib as mpl
    mpl.use('Agg')
    from matplotlib.backends.backend_pdf import PdfPages
    

    And then to print to file:

    #PDF build and save
        def multi_page(filename, figs=None, dpi=200):
            pp = PdfPages(filename)
            if figs is None:
                figs = [mpl.pyplot.figure(n) for n in mpl.pyplot.get_fignums()]
            for fig in figs:
                fig.savefig(pp, format='pdf', bbox_inches='tight', fig_size=(10, 8))
            pp.close()
    

    and to create the PDF:

    multi_page(report_name)
    

提交回复
热议问题