Generating a PNG with matplotlib when DISPLAY is undefined

前端 未结 12 1245
感动是毒
感动是毒 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:12

    To make sure your code is portable across Windows, Linux and OSX and for systems with and without displays, I would suggest following snippet:

    import matplotlib
    import os
    # must be before importing matplotlib.pyplot or pylab!
    if os.name == 'posix' and "DISPLAY" not in os.environ:
        matplotlib.use('Agg')
    
    # now import other things from matplotlib
    import matplotlib.pyplot as plt
    

    Credit: https://stackoverflow.com/a/45756291/207661

提交回复
热议问题