本文翻譯自:Generating a PNG with matplotlib when DISPLAY is undefined
I am trying to use networkx with Python. 我正在嘗試將networkx與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
#1樓
參考:https://stackoom.com/question/Bkte/未定義DISPLAY時-使用matplotlib生成PNG
#2樓
Just as a complement of Reinout's answer. 只是Reinout回答的補充。
The permanent way to solve this kind of problem is to edit .matplotlibrc file. 解決此類問題的永久方法是編輯.matplotlibrc文件。 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
. 然後將該文件中的backend : Agg
修改爲backend : Agg
。 That is it. 這就對了。
#3樓
I will just repeat what @Ivo Bosticky said which can be overlooked. 我將重複@Ivo Bosticky所說的話,但可以忽略。 Put these lines at the VERY start of the py file. 將這些行放在py文件的非常開頭。
import matplotlib
matplotlib.use('Agg')
Or one would get error 否則會出錯
*/usr/lib/pymodules/python2.7/matplotlib/__init__.py:923: 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,*
This will resolve all Display issue 這將解決所有顯示問題
#4樓
What system are you on? 您在什麼系統上? It looks like you have a system with X11, but the DISPLAY environment variable was not properly set. 看起來您的系統具有X11,但未正確設置DISPLAY環境變量。 Try executing the following command and then rerunning your program: 嘗試執行以下命令,然後重新運行程序:
export DISPLAY=localhost:0
#5樓
The main problem is that (on your system) matplotlib chooses an x-using backend by default. 主要問題是(在您的系統上)matplotlib默認情況下選擇使用x的後端。 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: 對我來說,解決方案是在其他任何pylab / matplotlib / pyplot導入之前先讀取的位置添加以下代碼:
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
The alternative is to set it in your .matplotlibrc 另一種方法是在您的.matplotlibrc中進行設置
#6樓
The clean answer is to take a little bit of time correctly prepare your execution environment. 明確的答案是花一些時間正確準備執行環境。
The first technique you have to prepare your execution environment is to use a matplotlibrc
file, as wisely recommended by Chris Q. , setting 準備執行環境的第一項技術是使用Chris Q.明智建議的matplotlibrc
文件,設置
backend : Agg
in that file. 在那個文件中。 You can even control — with no code changes — how and where matplotlib looks for and finds the matplotlibrc
file . 您甚至可以控制matplotlib的查找方式以及查找matplotlibrc
文件的方式,而無需更改代碼。
The second technique you have to prepare your execution environment is to use the MPLBACKEND
environment variable (and inform your users to make use of it): 準備執行環境的第二種方法是使用MPLBACKEND
環境變量 (並通知您的用戶使用它):
export MPLBACKEND="agg"
python <program_using_matplotlib.py>
This is handy because you don't even have to provide another file on disk to make this work. 這很方便,因爲您甚至不必在磁盤上提供另一個文件即可完成此工作。 I have employed this approach with, for example, testing in continuous integration, and running on remote machines that do not have displays. 我採用了這種方法,例如,在持續集成中進行測試,並在沒有顯示器的遠程計算機上運行。
Hard-coding your matplotlib backend to "Agg" in your Python code is like bashing a square peg into a round hole with a big hammer, when, instead, you could have just told matplotlib it needs to be a square hole. 在您的Python代碼中將matplotlib後端硬編碼爲“ Agg”,就像用大鐵錘將方形釘猛擊到圓孔中一樣,相反,您本可以告訴matplotlib它必須是方形孔。