DISPLAY未定义时使用matplotlib生成PNG [英] Generating a PNG with matplotlib when DISPLAY is undefined
本文介绍了DISPLAY未定义时使用matplotlib生成PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#!/ usr / bin / env python
导入networkx为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 ):
在< module>文件中的graph.py,第13行,
nx.draw(G)
文件/usr/lib/pymodules/python2.5/networkx/drawing/nx_pylab.py,第124行,绘制
cf = pylab.gcf ()
文件/usr/lib/pymodules/python2.5/matplotlib/pyplot.py,第276行,在gcf
返回数字()
文件/ usr / lib / pymodules /的python2.5 / matplotlib / pyplot.py 线254,在图
** kwargs)
档 /usr/lib/pymodules/python2.5/matplotlib/backends/backend_tkagg.py ,第90行,在new_figure_manager
window = Tk.Tk()
文件/usr/lib/python2.5/lib-tk/Tkinter.py,第1650行,在__init__
self.tk = _tkinter.create(screenName,baseName,className,interactive,wantobjects,useTk,sync,use)
_tkinter.TclError:没有显示名称和$ DISPLAY环境变量
$ c
现在我得到一个不同的错误:
#!/ usr / bin / env python
导入networkx为nx
导入matplotlib
导入matplotlib.pyplot
导入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。 (G)
plt.savefig(/ var / www / node.png)
< hr>
/usr/lib/pymodules/python2.5/matplotlib / __ init__.py:835:UserWarning:这次调用matplotlib.use( )没有效果
,因为后端已被选中;
在* pylab,matplotlib.pyplot,
或matplotlib.backends被首次导入之前,必须调用matplotlib.use()。
如果警告:warnings.warn(_use_error_msg)
Traceback(最近调用的最后一个):
在< module>文件中的第15行graph.py
nx.draw(G)
文件/usr/lib/python2.5/site-packages/networkx-1.2.dev-py2.5.egg/networkx/drawing/nx_pylab.py,第124行,绘制
cf = pylab.gcf()
文件/usr/lib/pymodules/python2.5/matplotlib/pyplot.py,第276行,在gcf
返回图()
文件/usr/lib/pymodules/python2.5/matplotlib/pyplot.py,第254行,在图
** kwargs)
文件/ usr / lib /pymodules/python2.5/matplotlib/backends/backend_tkagg.py,第90行,位于new_figure_manager
window = Tk.Tk()
文件/usr/lib/python2.5/lib-tk /Tkinter.py,第1650行,在__init__
self.tk = _tkinter.create(screenName,baseName,className,interactive,wantobjects,useTk,sync,use)
_tkinter.TclError:no display name并且没有$ DISPLAY环境变量
错误现在:
#!/ usr / bin / env python
导入networkx为nx
import matplotlib
import matplotlib.pyplot
im端口matplotlib.pyplot为plt
matplotlib.use('Agg')
G = nx.Graph()
G.add_node(1)
.gadd_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:这个对matplotlib.use()的调用没有效果
,因为后端已经被选中;
在* pylab,matplotlib.pyplot,
或matplotlib.backends被首次导入之前,必须调用matplotlib.use()。
如果警告:warnings.warn(_use_error_msg)
Traceback(最近调用的最后一个):
在< module>文件中的第15行graph.py
nx.draw(G)
文件/usr/lib/python2.5/site-packages/networkx-1.2.dev-py2.5.egg/networkx/drawing/nx_pylab.py,第124行,绘制
cf = pylab.gcf()
文件/usr/lib/pymodules/python2.5/matplotlib/pyplot.py,第276行,在gcf
返回图()
文件/usr/lib/pymodules/python2.5/matplotlib/pyplot.py,第254行,在图
** kwargs)
文件/ usr / lib /pymodules/python2.5/matplotlib/backends/backend_tkagg.py 线90,在new_figure_manager
窗口= Tk.Tk()
档 /usr/lib/python2.5/lib-tk /Tkinter.py,第1650行,在__init__
self.tk = _tkinter.create(screenName,baseName,className,interactive,wantobjects,useTk,sync,use)
_tkinter.TclError:no display name并且没有$ DISPLAY环境变量
解决方案主要问题是(在你的系统上)matplotlib默认选择一个x-use后端。我在我的一台服务器上遇到了同样的问题。对我来说,解决方案是将以下代码添加到 之前的任何其他pylab / matplotlib / pyplot 导入中:
import matplotlib
#强制matplotlib不使用任何Xwindows后端。
matplotlib.use('Agg')
另一种方法是将其设置为。 matplotlibrc
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
解决方案 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
这篇关于DISPLAY未定义时使用matplotlib生成PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文