在没有运行 X 服务器的情况下生成 matplotlib 图形
Matplotlib 似乎需要 $DISPLAY 环境变量,这意味着正在运行的 X 服务器。
某些 Web 托管服务不允许正在运行的 X 服务器会话。
有没有办法在没有正在运行的 X 服务器的情况下使用 matplotlib 生成图形?
[username@hostname ~]$ python2.6
Python 2.6.5 (r265:79063, Nov 23 2010, 02:02:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/username/lib/python2.6/matplotlib-1.0.1-py2.6-linux-i686.egg/matplotlib/pyplot.py", line 270, in figure
**kwargs)
File "/home/username/lib/python2.6/matplotlib-1.0.1-py2.6-linux-i686.egg/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/local/lib/python2.6/lib-tk/Tkinter.py", line 1643, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
>>>
Matplotlib seems to require the $DISPLAY environment variable which means a running X server.
Some web hosting services do not allow a running X server session.
Is there a way to generate graphs using matplotlib without a running X server?
[username@hostname ~]$ python2.6
Python 2.6.5 (r265:79063, Nov 23 2010, 02:02:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/username/lib/python2.6/matplotlib-1.0.1-py2.6-linux-i686.egg/matplotlib/pyplot.py", line 270, in figure
**kwargs)
File "/home/username/lib/python2.6/matplotlib-1.0.1-py2.6-linux-i686.egg/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/local/lib/python2.6/lib-tk/Tkinter.py", line 1643, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Neil的答案是一种(完全有效!)的方法,但您也可以 只需在导入
matplotlib.pyplot
之前调用matplotlib.use('Agg')
,并且然后照常继续。例如,
您也不必使用 Agg 后端。 pdf、ps、svg、agg、cairo 和 gdk 后端 都可以在没有 X 服务器的情况下使用。但是,默认情况下只会构建 Agg 后端(我认为?),因此很有可能在您的特定安装上可能未启用其他后端。
或者,您可以在
.matplotlibrc
文件自动让matplotlib.pyplot
使用给定的渲染器。@Neil's answer is one (perfectly valid!) way of doing it, but you can also simply call
matplotlib.use('Agg')
before importingmatplotlib.pyplot
, and then continue as normal.E.g.
You don't have to use the Agg backend, as well. The pdf, ps, svg, agg, cairo, and gdk backends can all be used without an X-server. However, only the Agg backend will be built by default (I think?), so there's a good chance that the other backends may not be enabled on your particular install.
Alternately, you can just set the backend parameter in your
.matplotlibrc
file to automatically havematplotlib.pyplot
use the given renderer.您需要直接使用 matplotlib API,而不是通过 pylab 界面。这里有一个很好的例子:
http://www.dalkescientific .com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html
You need to use the matplotlib API directly rather than going through the pylab interface. There's a good example here:
http://www.dalkescientific.com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html