{"id":1059,"hash":"5236c7c4102b279feac37b87b61706f9968a3bd16b78399588ed2348c11e5e84","pattern":"Generating a PNG with matplotlib when DISPLAY is undefined","full_message":"I am trying to use networkx with Python. When I run this program it get this error. Is there anything missing?\n\n#!/usr/bin/env python\n\nimport networkx as nx\nimport matplotlib\nimport matplotlib.pyplot\nimport matplotlib.pyplot as plt\n\nG=nx.Graph()\nG.add_node(1)\nG.add_nodes_from([2,3,4,5,6,7,8,9,10])\n#nx.draw_graphviz(G)\n#nx_write_dot(G, 'node.png')\nnx.draw(G)\nplt.savefig(\"/var/www/node.png\")\n\nTraceback (most recent call last):\n  File \"graph.py\", line 13, in <module>\n    nx.draw(G)\n  File \"/usr/lib/pymodules/python2.5/networkx/drawing/nx_pylab.py\", line 124, in draw\n    cf=pylab.gcf()\n  File \"/usr/lib/pymodules/python2.5/matplotlib/pyplot.py\", line 276, in gcf\n    return figure()\n  File \"/usr/lib/pymodules/python2.5/matplotlib/pyplot.py\", line 254, in figure\n    **kwargs)\n  File \"/usr/lib/pymodules/python2.5/matplotlib/backends/backend_tkagg.py\", line 90, in new_figure_manager\n    window = Tk.Tk()\n  File \"/usr/lib/python2.5/lib-tk/Tkinter.py\", line 1650, in __init__\n    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)\n_tkinter.TclError: no display name and no $DISPLAY environment variable\n\nI get a different error now:\n\n#!/usr/bin/env python\n\nimport networkx as nx\nimport matplotlib\nimport matplotlib.pyplot\nimport matplotlib.pyplot as plt\n\nmatplotlib.use('Agg')\n\nG=nx.Graph()\nG.add_node(1)\nG.add_nodes_from([2,3,4,5,6,7,8,9,10])\n#nx.draw_graphviz(G)\n#nx_write_dot(G, 'node.png')\nnx.draw(G)\nplt.savefig(\"/var/www/node.png\")\n\n/usr/lib/pymodules/python2.5/matplotlib/__init__.py:835: UserWarning:  This call to matplotlib.use() has no effect\nbecause the the backend has already been chosen;\nmatplotlib.use() must be called *before* pylab, matplotlib.pyplot,\nor matplotlib.backends is imported for the first time.\n\n  if warn: warnings.warn(_use_error_msg)\nTraceback (most recent call last):\n  File \"graph.py\", line 15, in <module>\n    nx.draw(G)\n  File \"/usr/lib/python2.5/site-packages/networkx-1.2.dev-py2.5.egg/networkx/drawing/nx_pylab.py\", line 124, in draw\n    cf=pylab.gcf()\n  File \"/usr/lib/pymodules/python2.5/matplotlib/pyplot.py\", line 276, in gcf\n    return figure()\n  File \"/usr/lib/pymodules/python2.5/matplotlib/pyplot.py\", line 254, in figure\n    **kwargs)\n  File \"/usr/lib/pymodules/python2.5/matplotlib/backends/backend_tkagg.py\", line 90, in new_figure_manager\n    window = Tk.Tk()\n  File \"/usr/lib/python2.5/lib-tk/Tkinter.py\", line 1650, in __init__\n    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)\n_tkinter.TclError: no display name and no $DISPLAY environment variable\n\nI get a different error now:\n\n#!/usr/bin/env python\n\nimport networkx as nx\nimport matplotlib\nimport matplotlib.pyplot\nimport matplotlib.pyplot as plt\n\nmatplotlib.use('Agg')\n\nG=nx.Graph()\nG.add_node(1)\nG.add_nodes_from([2,3,4,5,6,7,8,9,10])\n#nx.draw_graphviz(G)\n#nx_write_dot(G, 'node.png')\nnx.draw(G)\nplt.savefig(\"/var/www/node.png\")\n\n/usr/lib/pymodules/python2.5/matplotlib/__init__.py:835: UserWarning:  This call to matplotlib.use() has no effect\nbecause the the backend has already been chosen;\nmatplotlib.use() must be called *before* pylab, matplotlib.pyplot,\nor matplotlib.backends is imported for the first time.\n\n  if warn: warnings.warn(_use_error_msg)\nTraceback (most recent call last):\n  File \"graph.py\", line 15, in <module>\n    nx.draw(G)\n  File \"/usr/lib/python2.5/site-packages/networkx-1.2.dev-py2.5.egg/networkx/drawing/nx_pylab.py\", line 124, in draw\n    cf=pylab.gcf()\n  File \"/usr/lib/pymodules/python2.5/matplotlib/pyplot.py\", line 276, in gcf\n    return figure()\n  File \"/usr/lib/pymodules/python2.5/matplotlib/pyplot.py\", line 254, in figure\n    **kwargs)\n  File \"/usr/lib/pymodules/python2.5/matplotlib/backends/backend_tkagg.py\", line 90, in new_figure_manager\n    window = Tk.Tk()\n  File \"/usr/lib/python2.5/lib-tk/Tkinter.py\", line 1650, in __init__\n    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)\n_tkinter.TclError: no display name and no $DISPLAY environment variable","ecosystem":"pypi","package_name":"matplotlib","package_version":null,"solution":"The main problem is that (on your system) matplotlib chooses an x-using backend by default. I just had the same problem on one of my servers.  The solution for me was to add the following code in a place that gets read before any other pylab/matplotlib/pyplot import:\n\nimport matplotlib\n# Force matplotlib to not use any Xwindows backend.\nmatplotlib.use('Agg')\n\nThe alternative is to set it in your .matplotlibrc","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/2801882/generating-a-png-with-matplotlib-when-display-is-undefined","votes":337,"created_at":"2026-04-19T04:52:15.477178+00:00","updated_at":"2026-04-19T04:52:15.477178+00:00"}