Generating a PNG with matplotlib when DISPLAY is undefined

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

    The clean answer is to take a little bit of time correctly prepare your execution environment.

    The first technique you have to prepare your execution environment is to use a matplotlibrc file, as wisely recommended by Chris Q., setting

    backend : Agg
    

    in that file. You can even control — with no code changes — how and where matplotlib looks for and finds the matplotlibrc file.

    The second technique you have to prepare your execution environment is to use the MPLBACKEND environment variable (and inform your users to make use of it):

    export MPLBACKEND="agg"
    python 
    

    This is handy because you don't even have to provide another file on disk to make this work. I have employed this approach with, for example, testing in continuous integration, and running on remote machines that do not have displays.

    Hard-coding your matplotlib backend to "Agg" in your Python code is like bashing a square peg into a round hole with a big hammer, when, instead, you could have just told matplotlib it needs to be a square hole.

提交回复
热议问题