python - Generating a PNG with matplotlib when DISPLAY is undefined -
i trying use networkx python. when run program error. there missing?
#!/usr/bin/env python import networkx nx import matplotlib import matplotlib.pyplot import matplotlib.pyplot 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 , no $display environment variable
i different error now:
#!/usr/bin/env python import networkx nx import matplotlib import matplotlib.pyplot import matplotlib.pyplot plt matplotlib.use('agg') 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")
/usr/lib/pymodules/python2.5/matplotlib/__init__.py:835: userwarning: call matplotlib.use() has no effect because the backend has been chosen; matplotlib.use() must called *before* pylab, matplotlib.pyplot, or matplotlib.backends imported first time. if warn: warnings.warn(_use_error_msg) traceback (most recent call last): file "graph.py", line 15, in <module> nx.draw(g) file "/usr/lib/python2.5/site-packages/networkx-1.2.dev-py2.5.egg/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 , no $display environment variable
i different error now:
#!/usr/bin/env python import networkx nx import matplotlib import matplotlib.pyplot import matplotlib.pyplot plt matplotlib.use('agg') 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")
/usr/lib/pymodules/python2.5/matplotlib/__init__.py:835: userwarning: call matplotlib.use() has no effect because the backend has been chosen; matplotlib.use() must called *before* pylab, matplotlib.pyplot, or matplotlib.backends imported first time. if warn: warnings.warn(_use_error_msg) traceback (most recent call last): file "graph.py", line 15, in <module> nx.draw(g) file "/usr/lib/python2.5/site-packages/networkx-1.2.dev-py2.5.egg/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 , no $display environment variable
the main problem (on system) matplotlib chooses x-using backend default. had same problem on 1 of servers. solution me add following code in place gets read before other pylab/matplotlib/pyplot import:
import matplotlib # force matplotlib not use xwindows backend. matplotlib.use('agg')
the alternative set in .matplotlibrc
Comments
Post a Comment