未定义DISPLAY时,使用matplotlib生成PNG

当在没有DISPLAY环境变量的系统上使用matplotlib时,会出现错误。解决方法包括修改.matplotlibrc文件,将后端设置为'agg',或者在代码开始处指定后端,以及设置MATPLOTLIBRC环境变量。此外,确保正确配置执行环境也很重要。
摘要由CSDN通过智能技术生成

本文翻译自: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它必须是方形孔。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值