Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

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

import matplotlib

import matplotlib.pyplot

import matplotlib.pyplot as plt

G=nx.Graph()

G.add_node(1)

G.add_nodes_from([2,3,4,5,6,7,8,9,10])

#nx.draw_graphviz(G)

#nx_write_dot(G, 'node.png')

nx.draw(G)

plt.savefig("/var/www/node.png")

Traceback (most recent call last):

  File "graph.py", line 13, in <module>

    nx.draw(G)

  File "/usr/lib/pymodules/python2.5/networkx/drawing/nx_pylab.py", line 124, in draw

    cf=pylab.gcf()

  File "/usr/lib/pymodules/python2.5/matplotlib/pyplot.py", line 276, in gcf

    return figure()

  File "/usr/lib/pymodules/python2.5/matplotlib/pyplot.py", line 254, in figure

    **kwargs)

  File "/usr/lib/pymodules/python2.5/matplotlib/backends/backend_tkagg.py", line 90, in new_figure_manager

    window = Tk.Tk()

  File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 1650, in __init__

    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)

_tkinter.TclError: no display name and no $DISPLAY environment variable

1 Answer

0 votes
by (16.8k points)

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:

import matplotlib

# Force matplotlib to not use any Xwindows backend.

matplotlib.use('Agg')

The alternative is to set it in your .matplotlibrc

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...