Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a PNG with matplotlib when DISPLAY is undefined

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 

I get a different error now:

#!/usr/bin/env python  import networkx as nx import matplotlib import matplotlib.pyplot import matplotlib.pyplot as 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:  This call to matplotlib.use() has no effect because the the backend has already been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the 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 and no $DISPLAY environment variable 

I get a different error now:

#!/usr/bin/env python  import networkx as nx import matplotlib import matplotlib.pyplot import matplotlib.pyplot as 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:  This call to matplotlib.use() has no effect because the the backend has already been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the 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 and no $DISPLAY environment variable 
like image 742
krisdigitx Avatar asked May 10 '10 10:05

krisdigitx


People also ask

How can a plot be saved as a PNG image in matplotlib?

Method 1: Save Plot as Image with Matplotlib using savefig() fname : path or name of output file with extension. If extension is not provided plot is saved as png file.

Is PLT show () necessary?

Using plt. show() in Matplotlib mode is not required.

How do I make a matplotlib plot into an image?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

Why is my plot blank matplotlib?

The reason your plot is blank is that matplotlib didn't auto-adjust the axis according to the range of your patches. Usually, it will do the auto-adjust jobs with some main plot functions, such as plt. plot(), plt. scatter() ... .


2 Answers

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

like image 173
Reinout van Rees Avatar answered Oct 11 '22 16:10

Reinout van Rees


Just as a complement of Reinout's answer.

The permanent way to solve this kind of problem is to edit .matplotlibrc file. Find it via

>>> import matplotlib
>>> matplotlib.matplotlib_fname() # This is the file location in Ubuntu '/etc/matplotlibrc'

Then modify the backend in that file to backend : Agg. That is it.

like image 42
Chris.Q Avatar answered Oct 11 '22 14:10

Chris.Q